本文实例为大家分享了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配置
@H_403_24@<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <com.example.timertest.DragImageVIEw androID:ID="@+ID/div" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" /> </linearLayout>
控件的自绘代码
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); private RectF rcDst = new RectF(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(ArrayList<VIEw> vIEws) { // 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 UI控件之Gallery实现拖动式图片浏览效果Android中SeekBar拖动条控件使用方法详解Android控件拖动实例详解Android通过自定义ImageView控件实现图片的缩放和拖动的实现代码Android基于widget组件实现物体移动/控件拖动功能示例Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解Android使用WindowManager制作一个可拖动的控件Android编程之控件可拖动的实现方法 总结以上是内存溢出为你收集整理的Android如何创建可拖动的图片控件全部内容,希望文章能够帮你解决Android如何创建可拖动的图片控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)