小編給大家分享一下Android怎么創(chuàng)建可拖動(dòng)的圖片控件,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)建站成立以來不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點(diǎn),以客戶需求中心、市場(chǎng)為導(dǎo)向”的快速反應(yīng)體系。對(duì)公司的主營(yíng)項(xiàng)目,如中高端企業(yè)網(wǎng)站企劃 / 設(shè)計(jì)、行業(yè) / 企業(yè)門戶設(shè)計(jì)推廣、行業(yè)門戶平臺(tái)運(yùn)營(yíng)、重慶APP開發(fā)公司、手機(jī)網(wǎng)站開發(fā)、微信網(wǎng)站制作、軟件開發(fā)、成都聯(lián)通服務(wù)器托管等實(shí)行標(biāo)準(zhǔn)化操作,讓客戶可以直觀的預(yù)知到從創(chuàng)新互聯(lián)建站可以獲得的服務(wù)效果。
重載、自繪
1、從View派生一個(gè)控件類 ,構(gòu)造函數(shù)中調(diào)用父類構(gòu)造器。
2、重載其onDraw函數(shù),在里面繪制圖片。(和windows的MFC有種似曾相識(shí)的感覺,可能安卓借鑒了windows的模式吧)
消息處理
拖動(dòng)圖片的消息,主要是處理按下和移動(dòng)兩個(gè)消息,重載onTouchEvent。數(shù)學(xué)知識(shí)(平移):在ACTION_DOWN時(shí)記錄下坐標(biāo)點(diǎn),在ACTION_MOVE時(shí)根據(jù)當(dāng)前位置與按下時(shí)的位置算出平移量。刷新控件,導(dǎo)致控件重繪,重繪時(shí)移動(dòng)繪制的左上角坐標(biāo)即可。
剛開始時(shí),只是收到了ACTION_DOWN消息,ACTION_MOVE消息就是捕捉不到,上網(wǎng)搜了下,原來是我在onTouchEvent最后調(diào)用了父類函數(shù)return super.onTouchEvent(event);父類里面返回false表示對(duì)這些消息不予關(guān)注,后續(xù)的ACTION_MOVE和ACTION_UP就不會(huì)進(jìn)來了。
代碼和配置
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,則后續(xù)的move up 消息都不會(huì)觸發(fā)。 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函數(shù)是根據(jù)圖片尺寸計(jì)算適合屏幕居中的方法。
運(yùn)行程序
看完了這篇文章,相信你對(duì)“Android怎么創(chuàng)建可拖動(dòng)的圖片控件”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!