手指移动和放大缩小imageview

package com.dengfan;


import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;


import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SimpleAdapter.ViewBinder;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;


public class ImageZoom extends Activity implements OnTouchListener,View.OnClickListener 
{
/** Called when the activity is first created. */


Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();


PointF start = new PointF();
PointF mid = new PointF();
float oldDist;


private ImageView myImageView;


static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zoom);
myImageView = (ImageView) findViewById(R.id.zoom_img);
myImageView.setOnTouchListener(this);
myImageView.setImageBitmap(AppContext.zoomBitmap);
findViewById(R.id.zoom_set_to_wall).setOnClickListener(this);

SharedPreferences sharedPreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
boolean isFirstTimeUse = sharedPreferences.getBoolean("isfirsttimeuse", true);
Editor editor =  sharedPreferences.edit();
editor.putBoolean("isfirsttimeuse", false);
editor.commit();
if(isFirstTimeUse)
{
AlertDialog alertDialog  =new AlertDialog.Builder(this)
.setMessage("可以使用手指随意拖动和放大图片哦^^")
.setTitle("请用提醒")
.setPositiveButton("知道了", null)
.create();
alertDialog.show();
}
}


@Override
public void onClick(View v) 
{
switch (v.getId()) {
case R.id.zoom_set_to_wall:
try {
//AppContext.zoomBitmap = BitmapCompress.bitmapRoom(AppContext.zoomBitmap, AppContext.screenHeight);
setWallpaper(AppContext.zoomBitmap);
} catch (Exception e) {
e.printStackTrace();
}

break;


default:
break;
}
}

@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println(event.getPointerCount());
ImageView myImageView = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
matrix.set(myImageView.getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;


// ���ö�㴥��ģʽ
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
// ��ΪDRAGģʽ�������ƶ�ͼƬ
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
}
// ��ΪZOOMģʽ�����������
else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
// ����˶�ű����ͼƬ���е�λ��
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
myImageView.setImageMatrix(matrix);
return true;
}


// �����ƶ�����
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}


// �����е�λ��
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}

}



zoom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:gravity="center">
 <ImageView android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:scaleType="matrix"
  android:id="@+id/zoom_img" android:layout_centerInParent="true"/>
 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/zoom_set_to_wall" android:text="设为桌面" android:layout_alignParentLeft="true"
     android:layout_alignParentBottom="true"/>
</RelativeLayout>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值