Android中点击图片放大的简单方法


简单的思路就是把要放大的图片显示在一个对话框中显示出来

Java代码:

public void onThumbnailClick(View v) {
// final AlertDialog dialog = new AlertDialog.Builder(this).create();
// ImageView imgView = getView();
// dialog.setView(imgView);
// dialog.show();


// 全屏显示的方法
final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
ImageView imgView = getView();
dialog.setContentView(imgView);
dialog.show();


// 点击图片消失
imgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
private ImageView getView() {
ImageView imgView = new ImageView(this);
imgView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
InputStream is = getResources().openRawResource(R.drawable.thumbnail);
Drawable drawable = BitmapDrawable.createFromStream(is, null);
imgView.setImageDrawable(drawable);


return imgView;
}

布局文件:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


            android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/thumbnail"
        android:onClick="onThumbnailClick"/>


你可能感兴趣的:(Android中点击图片放大的简单方法)