<pre name="code" class="java">实体类: package com.example.showgetpic; import android.R.integer; class ImageBean { public String path; private int rightMargin; private int bottomMargin; public int getRightMargin() { return rightMargin; } public void setRightMargin(int rightMargin) { this.rightMargin = rightMargin; } public int getBottomMargin() { return bottomMargin; } public void setBottomMargin(int bottomMargin) { this.bottomMargin = bottomMargin; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } } BaseActivity: /** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.showgetpic; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; public class BaseActivity extends FragmentActivity { @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); } @Override protected void onResume() { super.onResume(); // onresume时,取消notification显示 // HXSDKHelper.getInstance().getNotifier().reset(); // umeng // MobclickAgent.onResume(this); } @Override protected void onStart() { super.onStart(); // umeng // MobclickAgent.onPause(this); } /** * 返回 * * @param view */ public void back(View view) { finish(); } }
package com.example.showgetpic;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.ArrayList;import android.R.integer;import android.content.Context;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.view.ViewGroup;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.LinearLayout;import
android.widget.LinearLayout.LayoutParams;import android.widget.Toast;/** * @author wangjing */public class MainActivity extends BaseActivity {private static final int SEL_PIC = 1;private Context context;private static int count =1;private LinearLayout linearLayout
= null;private ArrayList<ImageBean> imageBeans;private ImageView lv_image;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {context =
this;imageBeans = new ArrayList();linearLayout = (LinearLayout) findViewById(R.id.ly_list);findViewById(R.id.btn).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 本地相册选取if (count>5) {Toast.makeText(context, "最多上传5张", Toast.LENGTH_SHORT).show();return;}else
{count++;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) {ViewGroup.LayoutParams lps =new android.view.ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);lps.width = (int) ((IApplication.getScreenWidth(context)
- 20* IApplication.getScreenDensity(context) - 3 * 5 * IApplication.getScreenDensity(context)) / 4);lps.height = lps.width;img.setLayoutParams(lps);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();}}}布局:
布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" > <LinearLayout android:id="@+id/ly_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <ImageView android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/topic_plus_pressed" > </ImageView> </LinearLayout>
Application类: package com.example.showgetpic; import android.app.Application; import android.content.Context; import android.util.DisplayMetrics; import android.view.WindowManager; public class IApplication extends Application { //获取屏幕宽度 public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; return width; } //获取屏幕密度 public static float getScreenDensity(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); float density = outMetrics.density; return density; } /** * 获得屏幕高度 */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int height = outMetrics.heightPixels; return height; } /** * 获得状态栏的高度 */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } }