Question

In my Android application I need to place an icon on imageview at touch position of the image view. I used the following code but I could not get the result. My code is:

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;
            }
        });
    }

}

Expected output:

Whereever user tap the image view I need to place "P" icon on that.

enter image description here

Was it helpful?

Solution

I got the solution. This is the my full code.

// ------- Main class

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;
            }
        });
    }
}

// -------------- Customized image view class

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top