public class Main extends Activity{ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); final TextVIEw textVIEw = (TextVIEw)findVIEwByID(R.ID.textVIEw); final ImageVIEw image = (ImageVIEw) findVIEwByID(R.ID.imageVIEw2); //Bitmap Bitmap vIEwBitmap = Bitmap.createBitmap(image.getWIDth(),image.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(vIEwBitmap); image.draw(canvas); image.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { textVIEw.setText("touch coordinates : " + String.valueOf(event.getX()) + "x" + String.valueOf(event.getY())); return false; } }); }}
所以我想要做的是当用户触摸ImageVIEw时,图像将被准确地绘制在他触摸它的位置.
解决方法 您将要继承ImageVIEw以覆盖其onDraw()方法.通过这样做,您还可以在ontouchEvent()中进行自定义触摸处理,而不是附加侦听器.这不是一个完整的例子,但类似于以下内容:public class CustomImageVIEw extends ImageVIEw { private ArrayList<Point) mtouches; private Bitmap mMarker; //Java constructor public CustomImageVIEw(Context context) { super(context); init(); } //XML constructor public CustomImageVIEw(Context context,AttributeSet attrs) { super(context,attrs); init(); } private voID init() { mtouches = new ArrayList<Point>(); mMarker = BitmapFactory.decodeResource(context.getResources(),R.drawable.my_marker_image); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { //Capture a reference to each touch for drawing if(event.getAction() == MotionEvent.ACTION_DOWN) { mtouches.add( new Point(event.getX(),event.getY()) ); return true; } return super.ontouchEvent(event); } @OverrIDe protected voID onDraw(Canvas c) { //Let the image be drawn first super.onDraw(c); //Draw your custom points here Paint paint = new Paint(); for(Point p : mtouches) { c.drawBitmap(mMarker,p.x,p.y,paint); } }}
HTH!
总结以上是内存溢出为你收集整理的如何使用Canvas – Android将图像从drawable绘制到imageview上全部内容,希望文章能够帮你解决如何使用Canvas – Android将图像从drawable绘制到imageview上所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)