国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android如何創建可拖動的圖片控件

2019-10-22 18:13:38
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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配置

<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,則后續的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圖片控件,Android拖動控件

Android,拖動圖片控件,Android圖片控件,Android拖動控件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文安县| 纳雍县| 宝清县| 临澧县| 柳林县| 红桥区| 渭南市| 赤壁市| 丹凤县| 沙河市| 庆安县| 濉溪县| 中山市| 托克逊县| 浦江县| 正镶白旗| 图们市| 宣城市| 永寿县| 莒南县| 娄烦县| 黄梅县| 庆安县| 剑川县| 宁德市| 万荣县| 和平区| 嫩江县| 沙坪坝区| 南乐县| 仙居县| 金坛市| 合山市| 双流县| 东海县| 萍乡市| 铜梁县| 日喀则市| 瓦房店市| 盐山县| 军事|