Android 如何通过自定义左坐标和上坐标填充ImageView中的图像

Android 如何通过自定义左坐标和上坐标填充ImageView中的图像,android,imageview,android-recyclerview,coordinates,offset,Android,Imageview,Android Recyclerview,Coordinates,Offset,我需要显示帧内的一些图像(多个图像帧),我有左和顶坐标对应于每个图像 例如,我有图像坐标100(从左到右)和100(从上到右),因此我需要做的只是在图像视图中显示图像,该视图从(100100)开始 有谁能告诉我,如果没有位图,我怎么做呢?因为我需要在回收视图中显示包含多个图像的帧列表 我使用了TouchImageView.class的缩放和移动功能来创建帧 1。下面是我选择布局时的屏幕截图,您可以清楚地看到,这两幅图像彼此非常接近。我的意思是我选择了每个图像的一半。 2。接下来是主屏幕,我需要

我需要显示帧内的一些图像(多个图像帧),我有顶坐标对应于每个图像

例如,我有图像坐标100(从左到右)和100(从上到右),因此我需要做的只是在图像视图中显示图像,该视图从(100100)开始

有谁能告诉我,如果没有位图,我怎么做呢?因为我需要在回收视图中显示包含多个图像的帧列表

我使用了
TouchImageView.class
的缩放和移动功能来创建帧

1。下面是我选择布局时的屏幕截图,您可以清楚地看到,这两幅图像彼此非常接近。我的意思是我选择了每个图像的一半。

2。接下来是主屏幕,我需要在主屏幕上复制该图像,我有两幅图像的左坐标和上坐标。但是如何在ImageView中通过自定义x和y绘制该图像。

用于从左上方填充ImageView内部图像的代码片段-

 img.setPadding((int) x, (int) y, 0, 0);
但仍然不起作用

请给我一些建议,我做过谷歌搜索,但没有找到适合我要求的解决方案

非常感谢你的建议

非常感谢


问候。

听起来你想拍两张照片,然后根据这两张照片的某种组合创建一张新的照片

有很多不同的方法可以做到这一点。最直接的方法是使用位图。有两种主要的方法可以直接实现这一点

  • 在某个公共父对象中定位图像视图,并创建该父对象的位图以用作结果图像

  • 通过根据自己的计算绘制ImageView的图像,自己创建一个位图,该计算应模拟您拥有的布局结构

  • 由于您似乎已经在屏幕上以希望生成的图像看起来的方式显示了这两个图像,因此第一个图像将是最简单的,并且不需要您重做布局和图像视图已经执行的计算

    我们将要做的是利用这样一个事实,即每个视图都会将自己绘制到画布上,最终只是将自己转换为一个图像本身。要获得父布局的位图,只需创建一个相同大小的位图,然后让父布局在位图画布上绘制自己,如下所示:

    private Bitmap getCombinedBitmap(View parentView) {
        Bitmap result = Bitmap.createBitmap(parentView.getWidth(), parentView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        parentView.draw(canvas);
    
        return result;
    }
    
    但是,如果您不希望仅受父布局在屏幕上绘制的内容的约束,可以通过将图像绘制到类似的画布上来创建所需的位图。在这里,我实现了您问题中的第一个布局:

    private Bitmap getCombinedBitmap(Bitmap a, Bitmap b) {
        int aWidth = a.getWidth() / 2;
        int bWidth = b.getWidth() / 2;
        int resultHeight = a.getHeight() < b.getHeight()?  a.getHeight() : b.getHeight();
        Bitmap result = new Bitmap(aWidth + bWidth, resultHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
    
        Rect aRect = new Rect(0, 0, aWidth, resultHeight);
        Rect aResultRect = new Rect(0, 0, aWidth, resultHeight);
        canvas.drawBitmap(a, aRect, aResultRect);
    
        Rect bRect = new Rect(0, 0, bWidth, resultHeight);
        Rect bResultRect = new Rect(bWidth, 0, bWidth, resultHeight);
        canvas.drawBitmap(b, bRect, bResultRect);
    
        return result;
    }
    
    私有位图getCombinedBitmap(位图a、位图b){
    int aWidth=a.getWidth()/2;
    int bWidth=b.getWidth()/2;
    int-resultHeight=a.getHeight()
    在本例中,我选择了更清晰的代码,而不是更优化的代码。您可以通过不创建尽可能多的矩形等来优化这一点,并通常简化您的计算。但是您可以使用这种方法在单个位图上重新创建任何布局,您只需要更改计算

    我们要做的是首先创建一个位图,其宽度为第一个图像的一半加上第二个图像的一半,高度为两个图像中最小的一个。然后,我们从第一幅图像的最左边的半幅宽度方向和结果高度方向上取一部分,并将其绘制到结果上。然后,我们对第二幅图像执行相同的操作,但这次该部分是最右侧的半宽度方向


    希望这有帮助。

    更新:我在本文末尾添加了一个示例应用程序来说明这个概念。

    第二次更新:向示例应用程序添加了一些代码以适应xy缩放

    我已经更改了示例应用程序,使其包含的代码能够在考虑纵横比的情况下将图像(无论是小于还是大于
    ImageView
    )调整为视图大小。这可能不是你想要的,但它会让你进入一个大概的范围。这是示例代码,因此在实际应用程序中肯定还有其他需要考虑的问题。例如,
    xScale
    yScale
    在极端情况下可能为负值。我不确定如果他们这样做会发生什么,但可能会导致崩溃,因此需要进行边界检查


    如果我理解您的意图,您需要一个固定大小的ImageView来显示图像的不同部分 通过将图像的自定义(x,y)坐标设置为位于ImageView的(0,0)坐标,放大图像。 这基本上是对图像的上边缘和左边缘进行裁剪

    可能有很多不同的方法可以做到这一点,哪种方法最适合你取决于你的具体需求, 但有一种方法:

    在代码中使用以下内容或某些变体:

    float xOffset = -100;
    float yOffset = -100;
    ImageView imageView = (ImageView) findViewById(R.id.shiftedView);
    Matrix matrix = new Matrix();
    
    matrix.setTranslate(xOffset, yOffset);
    imageView.setImageMatrix(matrix);
    
    设置ImageView,如下所示。这里的关键是android:scaleType=“matrix”

    活动\u main.xml<ImageView android:id="@+id/shiftedView" android:layout_width="200dp" android:layout_height="match_parent" android:background="#FF00CCCC" android:scaleType="matrix" android:src="@drawable/image" />
    package com.example.imageshift;
    
    import android.graphics.Matrix;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        ImageView imageView;
        float xOffset = 0f;
        float yOffset = 0f;
        float xScale = 1.0f;
        float yScale = 1.0f;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            imageView = (ImageView) findViewById(R.id.shiftedView);
            findViewById(R.id.shiftUp).setOnClickListener(this);
            findViewById(R.id.shiftDown).setOnClickListener(this);
            findViewById(R.id.shiftLeft).setOnClickListener(this);
            findViewById(R.id.shiftRight).setOnClickListener(this);
            findViewById(R.id.shiftReset).setOnClickListener(this);
            findViewById(R.id.fitX).setOnClickListener(this);
            findViewById(R.id.fitY).setOnClickListener(this);
        }
    
        public void doMatrixTransformations() {
            Matrix matrix = new Matrix();
    
            matrix.setTranslate(xOffset, yOffset);
            matrix.postScale(xScale, yScale);
            imageView.setImageMatrix(matrix);
        }
    
        @Override
        public void onClick(View view) {
            final int shiftAmount = 50;
    
            switch (view.getId()) {
                case R.id.shiftUp:
                    yOffset -= shiftAmount;
                    break;
                case R.id.shiftDown:
                    yOffset += shiftAmount;
                    break;
    
                case R.id.shiftLeft:
                    xOffset -= shiftAmount;
                    break;
    
                case R.id.shiftRight:
                    xOffset += shiftAmount;
                    break;
    
                case R.id.fitX:
                    xScale = (float) imageView.getWidth() / (
                            (float) imageView.getDrawable().getIntrinsicWidth() + xOffset);
                    yScale = xScale;
                    break;
    
                case R.id.fitY:
                    yScale = (float) imageView.getHeight() /
                            ((float) imageView.getDrawable().getIntrinsicHeight() + yOffset);
                    xScale = yScale;
                    break;
    
                case R.id.shiftReset:
                    xOffset = 0;
                    yOffset = 0;
                    xScale = 1.0f;
                    yScale = 1.0f;
                    break;
            }
            doMatrixTransformations();
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.imageshift.MainActivity">
    
        <ImageView
            android:id="@+id/shiftedView"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:padding="2dp"
            android:scaleType="matrix"
            android:src="@drawable/image" />
    
        <Button
            android:id="@+id/shiftUp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Up" />
    
        <Button
            android:id="@+id/shiftDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/shiftUp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Down" />
    
        <Button
            android:id="@+id/shiftLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/shiftDown"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Left" />
    
        <Button
            android:id="@+id/shiftRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/shiftLeft"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Right" />
    
        <Button
            android:id="@+id/fitX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/shiftRight"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Fit X" />
    
        <Button
            android:id="@+id/fitY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/fitX"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Fit Y" />
    
        <Button
            android:id="@+id/shiftReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/fitY"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/shiftedView"
            android:text="Reset" />
    
    </RelativeLayout>
    
    <!-- we put the ImageView inside a Frame so that we can change -->
    <!--    the padding size to create borders -->
    <FrameLayout
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:id="@+id/image_frame"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/image_view"
            />
    </FrameLayout>
    
    FrameLayout frame = (FrameLayout)itemLayout.findViewById(R.id.image_frame);
    frame.setPadding((int) x, (int) y, 0, 0);
    
     Glide.with(context)
                    .load(R.drawable.raees)
                    .asBitmap()
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
    
                            // Add Bitmap in Array and load next bitmap
                            arts[0] = resource;
    
                            Glide.with(context)
                                    .load(R.drawable.raees_edited)
                                    .asBitmap()
                                    .into(new SimpleTarget<Bitmap>() {
                                        @Override
                                        public void onResourceReady(Bitmap resource1, GlideAnimation<? super Bitmap> glideAnimation) {
    
                                            // Both Bitmap loaded do the Merging
                                            arts[1] = resource1;
    
                                            //Set seekbars Max
                                            satrtX1SeekBar.setMax(arts[0].getWidth());
                                            satrtX2SeekBar.setMax(arts[1].getWidth());
                                            satrtY1SeekBar.setMax(arts[0].getHeight());
                                            satrtY2SeekBar.setMax(arts[1].getHeight());
    
                                            Bitmap bmOverlay = Bitmap.createBitmap(arts[0].getWidth(), arts[0].getHeight(), Bitmap.Config.ARGB_8888);
                                            Canvas canvas = new Canvas(bmOverlay);
    
                                            if (shouldDrawLeftHalf) {
                                                // Rect(left, top, right, bottom)
                                                canvas.drawBitmap(arts[0],
                                                        new Rect(0, 0, arts[0].getWidth() / 2, arts[0].getHeight()),
                                                        new Rect(0, 0, arts[0].getWidth() / 2, arts[0].getHeight()), null);
    
                                            } else {
                                                canvas.drawBitmap(arts[0], startX1, startY1, null);
                                            }
    
                                            if (shouldDrawRightHalf) {
                                                // Rect(left, top, right, bottom)
                                                canvas.drawBitmap(arts[1],
                                                        new Rect(arts[1].getWidth() / 2, 0, arts[0].getWidth(), arts[1].getHeight()),
                                                        new Rect(arts[1].getWidth() / 2, 0, arts[1].getWidth(), arts[1].getHeight()), null);
                                            } else {
                                                canvas.drawBitmap(arts[1], startX2, startY2, null);
                                            }
    
    
                                            background.setImageBitmap(bmOverlay);
    
                                            // saveToDisk("Test", bmOverlay);
                                        }
                                    });
                        }
                    });
    
     float newAspectFactor = 0;
    
    
     if (viewHeight > viewWidth) {
     //Aspect factor for Height
     newAspectFactor = viewHeight / bean.getPostMedia().get(i).getImg_height();
    
     } else {
     //Aspect factor for Width
     newAspectFactor = viewWidth / bean.getPostMedia().get(i).getImg_width();
     }
    
     float imgNewHeight = newAspectFactor * imageHeight;
     float imgNewWidth = newAspectFactor * imageWidth;
     Logger.logsError(TAG, " Image New Height : " + imgNewHeight);
     Logger.logsError(TAG, " Image New Width : " + imgNewWidth);
    
     if (imgNewHeight < viewHeight) {
     newAspectFactor = viewHeight / bean.getPostMedia().get(i).getImg_height();
     } else if (imgNewWidth < viewWidth) {
     newAspectFactor = viewWidth / bean.getPostMedia().get(i).getImg_width();
     }
    
                float x = 42.0;
                float y = 0.0;
    
                Matrix m = img.getImageMatrix();
                RectF drawableRect = new RectF(x,
                        y,
                        ((x * newAspectFactor) + viewWidth) / newAspectFactor,
                        imageHeight);
                RectF viewRect = new RectF(0,
                        0,
                        viewWidth,
                        viewHeight);
                m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL);
                img.setImageMatrix(m);
                img.setScaleType(ImageView.ScaleType.MATRIX);
                imageLoaderNew.displayImage("IMAGE URL", img, optionsPostImg);