標籤: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;@Overrideprotected 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() {@Overridepublic 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);}});}@Overrideprotected 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;}}}
主要學習點:對圖片的尺寸設定、布局的更新這兩塊。
Android 動態添加圖片 換行