本文介绍了如何在Android中的特定点在imageview上画一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在androidmanifest文件中的ImageView.

This is my ImageView in androidmanifest file.

<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="+id/imageview1"></ImageView>

我在触摸时在imageview上绘制了一个圆圈,但该圆圈绘制在左上角.

I drawn a circle on imageview on touch but the circle is drawn at left corner top.

这是我的代码:

ImageView myimage=(ImageView)findViewById(R.id.imageview1);
myimage.setImageResource(R.drawable.penguins);

BitmapDrawable bmpdraw=(BitmapDrawable)myimage.getDrawable();
Bitmap bmp=bmpdraw.getCopy().config();
Canvas m_canvas=new Canvas(bmp);


public void onTouch(View v,MotionEvent event){
int x=event.getX();
int y=event.getY();
canvas.drawCircle(x,y,10,paint);
myimage.setBitmap(bmp);

}

我知道画布的位置与imageview相比有所不同.

I came to know that canvas position is different when compared with imageview.

推荐答案

尝试遵循以下代码.my_canvas.xml

Try to following code .my_canvas.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


</RelativeLayout>

MyCanvas.java

MyCanvas.java

public class MyCanvas extends Activity {
    private RelativeLayout rl_Main;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_read);
        rl_Main = (RelativeLayout) findViewById(R.id.rl_main);
        rl_Main.addView(new MyView(this));
    }

    class MyView extends View{


        Paint paint = new Paint();
        Point point = new Point();
        public MyView(Context context) {
            super(context);
            paint.setColor(Color.RED);
            paint.setStrokeWidth(15);
            paint.setStyle(Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas) {
              Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.images);
              canvas.drawBitmap(b, 0, 0, paint);
              canvas.drawCircle(point.x, point.y, 100, paint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                point.x = event.getX();
                point.y = event.getY();

            }
              invalidate();
            return true;

        }

    }
     class Point {
            float x, y;
        }
}

这篇关于如何在Android中的特定点在imageview上画一个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:53