1. 程式人生 > >android 擊縮圖檢視大圖

android 擊縮圖檢視大圖

android中點選縮圖檢視大圖的方法一般有兩種,一種是想新浪微博list頁面那樣,彈出一個視窗顯示大圖(原activity為背景)。另一種就是直接開啟一個新的activity顯示大圖。 1、第一種方法我們可以使用自定義的AlertDialog來實現,程式碼如下: ImageView image=(ImageView)findViewById(R.id.small_image); image.setOnClickListener(new OnClickListener() { // 點選放大 public void onClick(View paramView) { LayoutInflater inflater = LayoutInflater.from(context); View imgEntryView = inflater.inflate(R.layout.dialog_photo_entry, null); // 載入自定義的佈局檔案 final AlertDialog dialog = new AlertDialog.Builder(context).create(); ImageView img = (ImageView)imgEntryView.findViewById(R.id.large_image); imageDownloader.download("圖片地址",img); // 這個是載入網路圖片的,可以是自己的圖片設定方法 dialog.setView(imgEntryView); // 自定義dialog
dialog.show(); // 點選佈局檔案(也可以理解為點選大圖)後關閉dialog,這裡的dialog不需要按鈕 imgEntryView.setOnClickListener(new OnClickListener() { public void onClick(View paramView) { dialog.cancel(); } }); } }); 對應的佈局檔案dialog_photo_entry內容為: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"    xmlns:android="http://schemas.android.com/apk/res/android">     <ImageView
android:layout_height="wrap_content" android:id="@+id/large_image " android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"> </ImageView> </RelativeLayout> 2、另外一種開啟一個新的activity的方法相對簡單點,大圖activity程式碼如下: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.dialog_photo_entry); activity = this; Bundle bundle = this.getIntent().getExtras(); if(bundle!=null){ picName = bundle.getString("picName"); //圖片名 } ImageView img = (ImageView)this.findViewById(R.id.large_image
 ); imageDownloader.download( picName,img); Toast toast = Toast.makeText(this, "點選圖片即可返回",Toast.LENGTH_SHORT); toast.setGravity(Gravity.BOTTOM, 0, 0); toast.show(); img.setOnClickListener(new View.OnClickListener() { // 點選返回 public void onClick(View paramView) { activity.finish(); } }); } 對應的佈局檔案dialog_photo_entry內容為: <?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"> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"     android:id="@+id/entry_pic" android:layout_margin="0dip" android:padding="0dip">    <ImageView android:layout_height="wrap_content" android:id="@+id/large_image"         android:layout_marginTop="0dip" android:paddingTop="0dip"             android:adjustViewBounds="true" android:layout_width="fill_parent"> </ImageView> </ScrollView> </LinearLayout>