Android怎么创建可拖动的图片控件-创新互联
小编给大家分享一下Android怎么创建可拖动的图片控件,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
创新互联公司专注于含山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供含山营销型网站建设,含山网站制作、含山网页设计、含山网站官网定制、重庆小程序开发公司服务,打造含山网络公司原创品牌,更为您提供含山网站排名全网营销落地服务。重载、自绘
1、从View派生一个控件类 ,构造函数中调用父类构造器。
2、重载其onDraw函数,在里面绘制图片。(和windows的MFC有种似曾相识的感觉,可能安卓借鉴了windows的模式吧)
消息处理
拖动图片的消息,主要是处理按下和移动两个消息,重载onTouchEvent。数学知识(平移):在ACTION_DOWN时记录下坐标点,在ACTION_MOVE时根据当前位置与按下时的位置算出平移量。刷新控件,导致控件重绘,重绘时移动绘制的左上角坐标即可。
刚开始时,只是收到了ACTION_DOWN消息,ACTION_MOVE消息就是捕捉不到,上网搜了下,原来是我在onTouchEvent最后调用了父类函数return super.onTouchEvent(event);父类里面返回false表示对这些消息不予关注,后续的ACTION_MOVE和ACTION_UP就不会进来了。
代码和配置
activity的XML配置
控件的自绘代码
package com.example.timertest; import java.util.ArrayList; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PointF; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; @SuppressLint("ClickableViewAccessibility") public class DragImageView extends View{ private Bitmap bmp = null; private PointF orgPos = new PointF(0, 0); private PointF downPos = new PointF(0, 0); private PointF movePos = new PointF(0, 0); private boolean bMove = false; private int nDstWidth = 0; private int nDstHeight = 0; private Rect rcSrc = new Rect(0, 0 , 0, 0); private RectF rcDst = new RectF(0, 0, 0, 0); private Paint paint = null; public DragImageView(Context context) { super(context); // TODO Auto-generated constructor stub paint = new Paint(Paint.ANTI_ALIAS_FLAG); //setOnClickListener(new DivOnClickListener()); //setOnTouchListener(l); } public DragImageView(Context context, AttributeSet attrs) { super(context, attrs); //bmp = img; paint = new Paint(Paint.ANTI_ALIAS_FLAG); } public DragImageView(Context context, AttributeSet attrs, int defStyleAttr){ super(context, attrs, defStyleAttr); paint = new Paint(Paint.ANTI_ALIAS_FLAG); } public void SetImage(Bitmap img){ if ( bmp != null ){ bmp = null; } bmp = img; } @Override public void addTouchables(ArrayListviews) { // TODO Auto-generated method stub super.addTouchables(views); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub float fPosX = event.getX(); float fPosY = event.getY(); int nAct = event.getAction(); switch ( nAct ){ case MotionEvent.ACTION_MOVE:{ if ( !bMove ) bMove = true; movePos.x = fPosX - downPos.x; movePos.y = fPosY - downPos.y; downPos.x = fPosX; downPos.y = fPosY; invalidate(); } break; case MotionEvent.ACTION_DOWN:{ downPos.x = fPosX; downPos.y = fPosY; } break; case MotionEvent.ACTION_UP: break; } //一定要返回ture,如果返回父类方法即false,则后续的move up 消息都不会触发。 return true; //return super.onTouchEvent(event); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if ( bmp == null ) return ; int nWidth = bmp.getWidth(); int nHeight = bmp.getHeight(); if ( !bMove ){ orgPos = GetCenterPos(); } else{ orgPos.x += movePos.x; orgPos.y += movePos.y; } rcSrc.right = nWidth; rcSrc.bottom = nHeight; rcDst.left = orgPos.x; rcDst.top = orgPos.y; rcDst.right = orgPos.x+nDstWidth; rcDst.bottom = orgPos.y+nDstHeight; canvas.drawBitmap(bmp, rcSrc, rcDst, paint); } protected PointF GetCenterPos(){ PointF pt = new PointF(0, 0); if ( bmp == null ) return pt; WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE); //wm.getDefaultDisplay().getSize(pt); int nScrWidth = wm.getDefaultDisplay().getWidth(); @SuppressWarnings("deprecation") int nScrHeight = wm.getDefaultDisplay().getHeight(); int nWidth = bmp.getWidth(); int nHeight = bmp.getHeight(); float fImgRate = nWidth/(float)nHeight; float fScrRate = nScrWidth/(float)nScrHeight; if ( nWidth>nScrWidth && nHeight>nScrHeight ){ if ( fImgRate > fScrRate ){ nDstWidth = nScrWidth; nDstHeight = (int)(nScrWidth/fImgRate); } else{ nDstHeight = nScrHeight; nDstWidth= (int)(nScrHeight*fImgRate); } } else if ( nWidth>nScrWidth ){ nDstWidth = nScrWidth; nDstHeight = nHeight; } else if ( nHeight>nScrHeight ){ nDstWidth = nWidth; nDstHeight = nScrHeight; } else{ nDstWidth = nWidth; nDstHeight = nHeight; } pt.y = (nScrHeight-nDstHeight)/2.0f; pt.x = (nScrWidth-nDstWidth)/2.0f; return pt; } }
其中GetCenterPos函数是根据图片尺寸计算适合屏幕居中的方法。
运行程序
看完了这篇文章,相信你对“Android怎么创建可拖动的图片控件”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
新闻名称:Android怎么创建可拖动的图片控件-创新互联
分享网址:http://scyanting.com/article/dsdosg.html