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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章