将位图放在android上的imageview上

时间:2014-02-23 14:57:35

标签: android android-drawable android-bitmap

在我的Android应用程序中,我需要在imageview的图像视图的触摸位置放置一个图标。我使用了以下代码,但无法得到结果。我的代码是:

public class MainActivity extends Activity {

    private ImageView iv;
    private Bitmap bitmap2 = null, bitmap = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Drawable myDrawable = getResources().getDrawable(R.drawable.i);
        bitmap2 = ((BitmapDrawable) myDrawable).getBitmap();

        Drawable myDrawable1 = getResources().getDrawable(
                R.drawable.ic_launcher);
        bitmap = ((BitmapDrawable) myDrawable1).getBitmap();
        final Bitmap bitmap1 = bitmap.copy(bitmap.getConfig(), true);

        iv = (ImageView) findViewById(R.id.imageView1);

        iv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                Matrix inverse = new Matrix();
                iv.getImageMatrix().invert(inverse);
                float[] touchPoint = new float[] { event.getRawX(),
                        event.getRawY() };
                inverse.mapPoints(touchPoint);
                Log.e("touch", "touch coords = [x,y]  " + touchPoint[0] + " , "
                        + touchPoint[1]);

                Bitmap resultBitmap = Bitmap.createBitmap(
                        bitmap1.getWidth(), bitmap1.getHeight(),
                        Bitmap.Config.ARGB_8888);

                Canvas c = new Canvas(resultBitmap);
                c.drawBitmap(bitmap1, touchPoint[0], touchPoint[1], null);
                Paint p = new Paint();
                p.setAlpha(127);
                c.drawBitmap(bitmap2, touchPoint[0], touchPoint[1], p);
                Log.e("canvas", "bitmap drawing");
                return false;
            }
        });
    }

}

预期产出:

当用户点击图片视图时,我需要在其上放置“P”图标。

enter image description here

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。这是我的完整代码。

// -------主类

public class MainActivity extends Activity {
    DrawImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (DrawImageView) findViewById(R.id.widgetMap);

        Drawable myDrawable = getResources().getDrawable(R.drawable.i);
        Bitmap bitmap2 = ((BitmapDrawable) myDrawable).getBitmap();
        DataPassing.bm = bitmap2;
        iv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                DrawImageView drawView = (DrawImageView) v;

                // set start coords
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    drawView.left = event.getX();
                    drawView.top = event.getY();
                    // set end coords
                } else {
                    drawView.right = event.getX();
                    drawView.bottom = event.getY();
                }
                // draw
                drawView.invalidate();
                drawView.drawRect = true;
                return true;
            }
        });
    }
}

// --------------自定义图像视图类

public class DrawImageView extends ImageView {
    private Paint currentPaint;
    public boolean drawRect = false;
    public float left;
    public float top;
    public float right;
    public float bottom;

    public DrawImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        currentPaint = new Paint();
        currentPaint.setDither(true);
        currentPaint.setColor(0xFF00CC00); // alpha.r.g.b
        currentPaint.setStyle(Paint.Style.STROKE);
        currentPaint.setStrokeJoin(Paint.Join.ROUND);
        currentPaint.setStrokeCap(Paint.Cap.ROUND);
        currentPaint.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (drawRect) {
            canvas.drawBitmap(DataPassing.bm, left, top, currentPaint);
        }
    }
}

// ----------- XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".MainActivity" >

    <com.example.imageondrawexample.DrawImageView
        android:id="@+id/widgetMap"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="@drawable/ic_launcher"
        android:drawingCacheQuality="auto" />

</RelativeLayout>