在这我把点击按钮缩放的代码贴上来,因为实在讨厌网上普遍流传的那种“替换imageiew的方法”。
主要代码如下
[java] view plain copy print ?
- import android.app.Activity;
- import android.graphics.Matrix;
- import android.graphics.PointF;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- public class MyImageViewActivity extends Activity {
- private ImageView view;
- new Matrix();
- Rect rect;
- private ImageButton zoom_in,zoom_out;
- private PointF mid;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- new PointF();
- findAll();
- setListener();
- }
- private void findAll(){
- view = (ImageView) findViewById(R.id.image_View);
- zoom_in = (ImageButton) findViewById(R.id.ibtn_zoom_in);
- zoom_out = (ImageButton) findViewById(R.id.ibtn_zoom_out);
- }
- private void setListener(){
- new MulitPointTouchListener());
- //放大
- new OnClickListener() {
- public void onClick(View v) {
- matrix.set(view.getImageMatrix());
- //设置放大的中心
- 1.3f, 1.3f, mid.x,mid.y);
- view.setImageMatrix(matrix);
- view.invalidate();
- }
- });
- //缩小
- new OnClickListener() {
- public void onClick(View v) {
- matrix.set(view.getImageMatrix());
- //设置放大的中心
- 0.8f, 0.8f, mid.x,mid.y);
- view.setImageMatrix(matrix);
- view.invalidate();
- }
- });
- }
- private void setMid(){
- rect = view.getDrawable().getBounds();
- mid.x = view.getDrawable().getBounds().centerX();
- mid.y = view.getDrawable().getBounds().centerY();
- }
- }
其中view.setOnTouchListener(new MulitPointTouchListener()); 就是上边那篇文章里的。
layout如下
[html] view plain copy print ?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/image_View"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="matrix"
- android:layout_centerInParent="true"
- android:src="@drawable/test_image" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:gravity="right"
- android:orientation="horizontal">
- <ImageButton
- android:id="@+id/ibtn_zoom_out"
- android:layout_width="66dp"
- android:layout_height="42dp"
- android:adjustViewBounds="true"
- android:background="#00000000"
- android:padding="0dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ibtn_gallery_zoom_out" />
- <ImageButton
- android:id="@+id/ibtn_zoom_in"
- android:layout_width="66dp"
- android:layout_height="42dp"
- android:adjustViewBounds="true"
- android:background="#00000000"
- android:padding="0dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ibtn_gallery_zoom_in" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
后面会把全部源码上传上来
最后效果如图: