Android 动态添加图片 换行

近在项目中用到动态添加图片,然后换行的实现。刚开始想用GridView,但是没用,什么原因到是忘了。下面我记录一下我的实现方式。
近在项目中用到动态添加图片,然后换行的实现。刚开始想用GridView,但是没用,什么原因到是忘了。下面我记录一下我的实现方式。
看代码xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sel_pic_6" />

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

Activity:

package org.cn.ruimit.pic;  
  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.util.ArrayList;  
  
import org.cn.ruimit.R;  
  
import android.content.Intent;  
import android.database.Cursor;  
import android.graphics.BitmapFactory;  
import android.net.Uri;  
import android.os.Bundle;  
import android.provider.MediaStore;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.ImageView;  
import android.widget.ImageView.ScaleType;  
import android.widget.LinearLayout;  
import android.widget.LinearLayout.LayoutParams;  
  
import com.china.hunbohui.BaseActivity;  
import com.china.hunbohui.IApplication;  
  
/** 
 * @author wangjing 
 */  
public class AddPicAty extends BaseActivity {  
  
    private static final int SEL_PIC = 1;  
  
    private LinearLayout linearLayout = null;  
    private ArrayList<ImageBean> imageBeans;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.add_pic_lay);  
        initView();  
    }  
  
    private void initView() {  
        imageBeans = new ArrayList<AddPicAty.ImageBean>();  
        linearLayout = (LinearLayout) findViewById(R.id.linearlayout);  
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                // 本地相册选取   
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"   
                // intent.addCategory(Intent.CATEGORY_OPENABLE);   
                intent.setType("image/*");  
                startActivityForResult(intent, SEL_PIC);  
            }  
        });  
    }  
  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);  
        if (resultCode == RESULT_OK) {  
            if (requestCode == SEL_PIC) {  
                Uri uri = data.getData();  
                String[] proj = { MediaStore.Images.Media.DATA };  
                Cursor cursor = managedQuery(uri, proj, null, null, null);  
                // 按我个人理解 这个是获得用户选择的图片的索引值   
                int column_index = cursor  
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
                // 将光标移至开头 ,这个很重要,不小心很容易引起越界   
                cursor.moveToFirst();  
                // 最后根据索引值获取图片路径   
                String path = cursor.getString(column_index);  
                ImageBean bean = new ImageBean();  
                bean.setPath(path);  
                imageBeans.add(bean);  
                updateLayout();  
            }  
        }  
    }  
      
    /**  
     * 更新图片布局  
     */  
    private void updateLayout(){  
        LinearLayout ll_horizontal = null;  
        linearLayout.removeAllViews();  
        for (int i = 0; i < imageBeans.size(); i++) {  
            if (i%4 == 0) {  
                ll_horizontal = new LinearLayout(context);  
                ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);  
                linearLayout.addView(ll_horizontal);  
            }  
            ImageView img = new ImageView(context);  
            setImgLayoutParams(img);  
            setImageBitmap(img, imageBeans.get(i).getPath());  
            ll_horizontal.addView(img);  
        }  
    }  
    /** 
     * 设置imageview的尺寸 
     */  
    private void setImgLayoutParams(ImageView img) {  
            // LayoutParams lp = (LayoutParams) img.getLayoutParams();   
            LayoutParams lp = new LayoutParams(0, 0);  
            lp.width = (int) ((IApplication.with - 20 * IApplication.dencity - 3 * 5 * IApplication.dencity) / 4);  
            lp.height = lp.width;  
            lp.rightMargin = (int) (5 * IApplication.dencity);  
            lp.bottomMargin = (int) (5 * IApplication.dencity);  
            img.setLayoutParams(lp);  
            img.setScaleType(ScaleType.CENTER_CROP);  
    }  
    /** 
     * 为imageview设置图片 
     */  
    private void setImageBitmap(ImageView img,String url){  
         try {  
              FileInputStream fis = new FileInputStream(url);  
              img.setImageBitmap(BitmapFactory.decodeStream(fis));  
         } catch (FileNotFoundException e) {  
              e.printStackTrace();  
         }  
    }  
  
    /** 
     * 图片对象 
     */  
    class ImageBean {  
        private String path;  
        public String getPath() {  
            return path;  
        }  
        public void setPath(String path) {  
            this.path = path;  
        }  
    }  
  
}  

主要学习点:对图片的尺寸设置、布局的更新这两块。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈振阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值