从本地相册中动态添加image

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

}

时间: 2024-08-29 18:02:51

从本地相册中动态添加image的相关文章

android 在布局中动态添加控件

第一步 Java代码 final LayoutInflater inflater = LayoutInflater.from(this); 第二步:获取需要被添加控件的布局 Java代码 final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01); 第三步:获取需要添加的布局(控件) Java代码 LinearLayout layout = (LinearLayout) inflater.inflate( R

Android 在布局容器中动态添加控件

这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉已有的控件 3.使用,来个小demo就明白了 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save

在CFormView或对话框中动态添加CScrollView、CFormView

在CFormView或对话框中动态添加CScrollView.CFormView 本代码可以在CFormView中,根据事先画好的控件位置创建CScrollView 也可以在CDialog中创建CScrollView.CFormView等 注: 若以下代码放在CMainRightView::OnCreate(LPCREATESTRUCT lpCreateStruct)内,则GetDlgItem()函数将调用失败,因为此时控件都还未被创建! void CMainRightView::OnIniti

Android在布局中动态添加view的两种方法

一.说明 添加视图文件的时候有两种方式:1.通过在xml文件定义layout:2.java代码编写 二.前言说明 1.构造xml文件 2.LayoutInflater 提到addview,首先要了解一下LayoutInflater类.这个类最主要的功能就是实现将xml表述的layout转化为View的功能.为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体widget控件实例化,而LayoutI

html页面下拉列表中动态添加后台数据(格式化数据,显示出数据的层次感)

html页面下拉列表中动态添加后台数据(格式化数据,显示出数据的层次感) 效果图: 运行原理和技术: 当页面加载完毕,利用jquery向后台发送ajax请求,去后台拼接<select></select>中的option字符串.让后将字符串响应回来,动态添加到<select>中.其中的字符串中包含了后台的数据. 页面js代码: 1 <script type="text/javascript"> 2 //加载部门 3 function loa

vue中动态添加div

知识点:vue中动态添加div节点,点击添加,动态生成div,点击删除,删除对应的div,其中数组的长度是动态改变的,如在from表单中应用,直接在提交方法中,获得list,获取所填的元素即可 效果: 核心代码说明(样式代码可自行修改,详细代码请参照源码): <div v-for="(v,i) in list"> <div class="form-group m-form__group row" style="padding-top: 1

ASP.NET中Literal控件的使用方法(用于向网页中动态添加内容)

原文:https://www.jb51.net/article/82855.htm 可以将 Literal 控件用作网页上其他内容的容器.Literal 控件最常用于向网页中动态添加内容.简单的讲,就是可以把 HTML 代码写到 Literal 控件上,直接呈现出来. 一.常见Literal属性 属性 描述 Text 指定 Literal 控件中显示的文本.在用户的浏览器中,这会显示为 HTML. Mode 指定控件如何处理添入其中的标记. 二.基础用法 前台 LiteralTest.aspx

如何获取jQuery中动态添加的元素

一.问题描述 用jQuery的append()方法动态添加了一段html代码之后,发现在为新添加的元素绑定click事件时无法获取该新元素. 二.解决方法 度娘推荐的方法基本是用live()方法 live()的官方定义和用法: live() 方法为被选元素附加一个或多个事件处理程序,并规定当这些事件发生时运行的函数.通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素). live()的详细使用方法可以查看jQuery live() live()和

Unity NGUI中动态添加和删除sprite

转自:http://www.cnblogs.com/vitah/p/3897664.html (以后,参考链接和作者将在文章首部给出,转载请保留此部分内容) 参考链接:http://www.narkii.com/club/thread-299977-1.html,作者:纳金网 比巴卜: 参考链接:http://game.ceeger.com/forum/read.php?tid=2852,作者:Unity圣典论坛 kuku小夭 动态添加和删除Sprite可以在很多地方用到,这里以实现显示技能CD