[color=brown]Activity:[/color]
[color=brown]xml:[/color]
[color=brown]Manifast,colors.xml,styles.xml:[/color]
package com.bocom;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivityImage extends Activity implements OnTouchListener {
private ImageView imgview;
private ImageView img;
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_activity_image);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
imgview = (ImageView) this.findViewById(R.id.imag1);
// imgview.setAnimation(AnimationUtils.loadAnimation(this,
// R.anim.newanim));
// img = (ImageView) this.findViewById(R.id.imag);
// Matrix mt = img.getImageMatrix();
// // mt.postRotate(30);
// mt.postScale(0.5f, 0.5f);
// mt.postScale(1.5f, 1.5f);
// mt.postRotate(30, 130, 100);
// mt.postTranslate(100, 10);
//
// img.setImageMatrix(mt);
// imgview.setLongClickable(true);
String v=getIntent().getStringExtra("iv");
if("1".equals(v))
imgview.setImageBitmap(BitmapFactory.decodeResource(
this.getResources(), R.drawable.i1_1));
if("2".equals(v))
imgview.setImageBitmap(BitmapFactory.decodeResource(
this.getResources(), R.drawable.i1_2));
imgview.setOnTouchListener(this);
imgview.setLongClickable(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);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
/*
* Log.d("Infor", "类别:"+event.getAction()); Log.d("Infor",
* "mask:"+event.getActionMasked()); Log.d("Infor",
* "index:"+event.getActionIndex()); Log.d("Infor",
* "points:"+event.getPointerCount());
*/
ScaleType type=ScaleType.MATRIX;
imgview.setScaleType(type);
Log.d("Infor", "size:" + event.getSize());
if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP)
Log.d("Infor", "多点操作");
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
matrix.set(imgview.getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d("Infor", "触摸了...");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN: // 多点触控
oldDist = this.spacing(event);
if (oldDist > 10f) {
Log.d("Infor", "oldDist" + oldDist);
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) { // 此实现图片的拖动功能...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
} else if (mode == ZOOM) {// 此实现图片的缩放功能...
float newDist = spacing(event);
if (newDist > 10) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
imgview.setImageMatrix(matrix);
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_activity_image, menu);
return true;
}
}
[color=brown]xml:[/color]
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/linearLayout1"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imag1"
android:scaleType="center"
></ImageView>
</RelativeLayout>
[color=brown]Manifast,colors.xml,styles.xml:[/color]
<activity
android:name=".MainActivityImage"
android:theme="@style/Transparent"
android:label="@string/title_activity_main_activity_image" >
</activity>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#9000</color>
</resources>
<style name="Transparent">
<item name="android:windowBackground">@color/transparent</item>
<item name ="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>