Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 動態添加圖片 換行

Android 動態添加圖片 換行

編輯:關於Android編程

近在項目中用到動態添加圖片,然後換行的實現。剛開始想用GridView,但是沒用,什麼原因到是忘了。下面我記錄一下我的實現方式。

看代碼xml:




    

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

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_pic_lay);
		initView();
	}

	private void initView() {
		imageBeans = new ArrayList();
		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. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved