標籤:android style blog http java color
網上有許多關於圖片縮放的demo,本人都感覺不怎麼好用,最近在github看到了 一個簡單的支援多指縮放圖片的Android View類 gesture-imageview (地址:https://github.com/jasonpolites/gesture-imageview),感覺還挺好用的,現在寫個demo方便以後用於調用
第一步:添加庫,推薦直接下載zip,匯入工程後,直接將main裡的com.polites.android包直接複製到自己的工程中,方便自己以後修改
第二步:由於我只需用到GestureImageView類就可以了,所以本例子只調用了這個類
以下是代碼:MainActivity.java
package com.example.android_imageview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.polites.android.GestureImageView;public class MainActivity extends Activity implements OnClickListener{ protected GestureImageView view; Button btn_save,btn_close; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = (GestureImageView) findViewById(R.id.gestureimage_image); //本例子只用本地圖片作為樣本圖片,圖片可以任意改 view.setImageResource(R.drawable.ic_launcher); } @Override public void onClick(View arg0) { if(arg0.getId() == R.id.btn_save){ //儲存圖片的method }else if(arg0.getId() == R.id.btn_close){ //關閉Activity finish(); } }}
布局檔案:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:gesture-image="http://schemas.polites.com/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <com.polites.android.GestureImageView android:id="@+id/gestureimage_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" gesture-image:min-scale="0.75" gesture-image:max-scale="10.0"/> <Button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="save"/> <Button android:id="@+id/btn_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="close"/></RelativeLayout>
注意:RelativeLayout中的 xmlns:gesture-image=http://schemas.polites.com/android必須要添加,不然得話會報錯,以下三個屬性為控製圖片縮放的
gesture-image:min-scale 縮放最小值gesture-image:max-scale 縮放最大值gesture-image:strict 是否精確
附加一句:GestureImageView使用方式和 ImageView 的使用方式是一樣的。
各位也可看一下源碼,更清楚一點