关闭

手指移动和放大缩小imageview

标签: androidmatrixlayoutfloatexceptionencoding
2003人阅读 评论(0) 收藏 举报
分类:
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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:225659次
    • 积分:3082
    • 等级:
    • 排名:第11684名
    • 原创:80篇
    • 转载:22篇
    • 译文:0篇
    • 评论:38条
    文章分类
    最新评论