zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    webpack中optimization 的 runtimeChunk 是干嘛的
    快速排序
    域名解析的设置
    MingW和cygwin的区别(转)
    设计模式总结
    OPTIONS请求 简单请求与 非简单请求
    axios的坑
    idea+maven+springboot+mybatis+springmvc+shiro
    Shiro 自定义角色 认证
    spring+shiro+springmvc+maven权限卡控示例
  • 原文地址:https://www.cnblogs.com/qhyhao/p/3757211.html
Copyright © 2011-2022 走看看