小編給大家分享一下Android怎么創(chuàng)建可拖動的圖片控件,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)公司專注于含山網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供含山營銷型網(wǎng)站建設,含山網(wǎng)站制作、含山網(wǎng)頁設計、含山網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)公司服務,打造含山網(wǎng)絡公司原創(chuàng)品牌,更為您提供含山網(wǎng)站排名全網(wǎng)營銷落地服務。重載、自繪
1、從View派生一個控件類 ,構造函數(shù)中調(diào)用父類構造器。
2、重載其onDraw函數(shù),在里面繪制圖片。(和windows的MFC有種似曾相識的感覺,可能安卓借鑒了windows的模式吧)
消息處理
拖動圖片的消息,主要是處理按下和移動兩個消息,重載onTouchEvent。數(shù)學知識(平移):在ACTION_DOWN時記錄下坐標點,在ACTION_MOVE時根據(jù)當前位置與按下時的位置算出平移量。刷新控件,導致控件重繪,重繪時移動繪制的左上角坐標即可。
剛開始時,只是收到了ACTION_DOWN消息,ACTION_MOVE消息就是捕捉不到,上網(wǎng)搜了下,原來是我在onTouchEvent最后調(diào)用了父類函數(shù)return super.onTouchEvent(event);父類里面返回false表示對這些消息不予關注,后續(xù)的ACTION_MOVE和ACTION_UP就不會進來了。
代碼和配置
activity的XML配置
<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 , 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(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,則后續(xù)的move up 消息都不會觸發(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ù)圖片尺寸計算適合屏幕居中的方法。
運行程序
看完了這篇文章,相信你對“Android怎么創(chuàng)建可拖動的圖片控件”有了一定的了解,如果想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章題目:Android怎么創(chuàng)建可拖動的圖片控件-創(chuàng)新互聯(lián)
新聞來源:http://www.rwnh.cn/article36/dsdosg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管、企業(yè)建站、用戶體驗、營銷型網(wǎng)站建設、App設計、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容