標籤:android c style class blog http
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); // 自訂dialogdialog.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>