Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android如何创建可拖动的图片控件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了Android创建可拖动图片控件的具体代码,供大家参,具体内容如下

重载、自绘

1、从View派生一个控件类 ,构造函数调用父类构造器。

2、重载其onDraw函数,在里面绘制图片。(和windows的MFC有种似曾相识的感觉,可能安卓借鉴了windows的模式吧)

消息处理

拖动图片的消息,主要是处理按下和移动两个消息,重载onTouchEvent。数学知识(平移):在ACTION_DOWN时记录下坐标点,在ACTION_MOVE时根据当前位置与按下时的位置算出平移量。@R_403_1864@,导致控件重绘,重绘时移动绘制的左上角坐标即可。

刚开始时,只是收到了ACTION_DOWN消息,ACTION_MOVE消息就是捕捉不到,上网搜了下,原来是我在onTouchEvent最后调用父类函数return super.onTouchEvent(event);父类里面返回false表示对这些消息不予关注,后续的ACTION_MOVE和ACTION_UP就不会进来了。

代码和配置

activity的XML配置

<LinearLayout xmlns:android="http://scheR_368_11845@as.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如何创建可拖动的图片控件

Android如何创建可拖动的图片控件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android如何创建可拖动的图片控件全部内容,希望文章能够帮你解决Android如何创建可拖动的图片控件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:拖动控件