标签:des android style blog http io os java ar
<strong>近在项目中用到动态添加图片,然后换行的实现。刚开始想用GridView,但是没用,什么原因到是忘了。下面我记录一下我的实现方式。</strong>
<strong> </strong>
看代码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; } } }
标签:des android style blog http io os java ar
原文地址:http://blog.csdn.net/laosanwangjing/article/details/39318039