package com.carbs.testandroidimage; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements View.OnClickListener { private ImageView iv; private Button bt1_refine; private Button bt2_zoomin; private int iv_height = 0; private int iv_width = 0; private int dr_height = 0; private int dr_width = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); iv = (ImageView) this.findViewById(R.id.iv); bt1_refine = (Button) this.findViewById(R.id.bt1); bt2_zoomin = (Button) this.findViewById(R.id.bt2); bt1_refine.setOnClickListener(this); bt2_zoomin.setOnClickListener(this); iv.post( new Runnable() { @Override public void run() { //獲取imageview的寬和高、獲取drawable的寬和高 Drawable d = iv.getDrawable(); //drawable的寬高,存儲到全局變量中 dr_width = d.getIntrinsicWidth(); dr_height = d.getIntrinsicHeight(); //imageview的寬高,存儲到全局變量中 iv_height = iv .getMeasuredHeight(); iv_width = iv.getMeasuredWidth(); Log. d("a", "onClick() --> dr_width is " + dr_width + " | dr_height is " + dr_height + " | iv_height is " + iv_height + " | iv_width is " + iv_width); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt1 : if((iv_height * dr_width > dr_height * iv_width)){ //若是iv更細高,也就是咱們須要調整其中圖片的高度,使其高度和imageview的高度一致,這樣算出scale後,使drawable的寬高同時放大scale倍。因爲matrix模式下,調整scale後imageview顯示圖片依然是從左上角開始顯示的,所以須要調整imageview的左右平移,使顯示的drawable正好位於正中間。另外一種狀況(若是iv更寬扁)同理 //計算出高度須要放大多少倍 float scale1 = ((float)iv_height )/((float)dr_height); //計算出左右平移多少 float offset1 = (dr_width *scale1 - (float)iv_width )/2; Log. d("1223", "offset1 is " + offset1); Matrix matrix1 = new Matrix(); matrix1.postScale(scale1, scale1); matrix1.postTranslate(-offset1, 0); iv.setImageMatrix(matrix1); } else{ //若是iv更寬扁,同理 float scale2 = ((float)iv_width )/((float)dr_width); float offset2 = (dr_height *scale2 - (float)iv_height )/2; Log. d("1223", "offset2 is " + offset2); //產生新的大小但Bitmap對象 Matrix matrix2 = new Matrix(); matrix2.postScale(scale2, scale2); matrix2.postTranslate(0, -offset2); iv.setImageMatrix(matrix2); } break; case R.id.bt2 : Matrix ma = iv.getImageMatrix(); float[] dValues = new float[9]; ma.getValues(dValues); dValues[0] = dValues[0] + 0.1f; dValues[4] = dValues[4] + 0.1f; float offsetheight = (dr_height *dValues[0] - (float)iv_height )/2; dValues[5] = - offsetheight; float offsetwidth = (dr_width *dValues[0] - (float)iv_width )/2; dValues[2] = - offsetwidth; Matrix m = new Matrix(); m.setValues(dValues); iv.setImageMatrix(m); break; } } public String printMyMatrix(Matrix m){ String s = ""; float[] valueFloat = new float[9]; m.getValues(valueFloat); for(int i = 0; i < 9; i++){ s = s + " [ " + valueFloat[i] + " ] " ; } return s; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:gravity= "center_horizontal" android:orientation= "vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" > <ImageView android:id="@+id/iv" android:layout_width="1200px" android:layout_height="700px" android:background="@drawable/background_stroke" android:scaleType="matrix" android:src="@drawable/dog" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 1" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 2" /> </LinearLayout > </LinearLayout>