当前位置:编程学习 > html/css >>

图片拖动功能实现

XML文件
 
Java代码 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ImageView   
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:scaleType="matrix" 
    android:id="@+id/imageView" 
    android:src="@drawable/hd3" 
    /> 
</LinearLayout> 
 
主页面:MainActivity.java
 
Java代码 
package com.xiawenquan; 
 
import android.app.Activity; 
import android.graphics.Matrix; 
import android.graphics.PointF; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private ImageView imageView; 
    private PointF startPoint = null; 
    private Matrix matrix = null; 
    private Matrix currMatrix ; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        startPoint = new PointF();  
        matrix = new Matrix(); // 用于拖拉变换 
        currMatrix = new Matrix(); // 用于拖拉变换 
         
         
        imageView = (ImageView) findViewById(R.id.imageView); 
        imageView.setOnTouchListener(new View.OnTouchListener() { 
             
            @Override 
            public boolean onTouch(View view, MotionEvent event) { 
                switch (event.getAction() & MotionEvent.ACTION_MASK) { 
                case MotionEvent.ACTION_DOWN://只有一个手指按下屏幕时触发的事件 
                    currMatrix.set(matrix); // 保存上一次的移动点 
                    startPoint.set(event.getX(),event.getY());//移动前,手指按下的位置 
                     
                     
                    break; 
                case MotionEvent.ACTION_MOVE://手指在屏幕移动时触发的事件,该事件在移动过程不断被触发 
                    float mX = event.getX() - startPoint.x; // 移动点的X坐标 
                    float mY = event.getY() - startPoint.y;// 移动点的Y坐标 
                    matrix.set(currMatrix); // 保存当前变换的点 
                    matrix.postTranslate(mX, mY); // 移动到指定的点 
                     
                     
                     
                     
                    break; 
                     
                case MotionEvent.ACTION_POINTER_DOWN://如果已经有一个手指按下屏幕,后续再有手指按下屏幕,就会触发这个事件 
                     
                    break; 
                     
                case MotionEvent.ACTION_UP://最后一个手指离开屏幕后触发的事件 
                     
                    break; 
                case MotionEvent.ACTION_POINTER_UP://有一个手指离开了屏幕,但还有手指在屏幕上,此时会触发该事件 
                     
                    break; 
 
                default: 
                    break; 
                } 
                 
         &n

补充:web前端 , HTML/CSS  ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,