图片拖动功能实现
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());//移动前,手指按下的位置
相关新闻>>
- 发表评论
-
- 最新评论 更多>>