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

来源:互联网 发布:百度云 知乎 编辑:程序博客网 时间:2024/06/11 18:47


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

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;
}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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" >


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


原创粉丝点击