Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等 - Go语言中文社区

Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等


仿照微信,朋友圈分享图片功能 。可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 。很不错的源码,大家有需要可以下载看看 。

下载地址 : 微信上传图片源码

很多网友不知道怎么获取图片路径,这里贴出来:

String path = Bimp.tempSelectBitmap.get(position).getImagePath();

//部分代码如下

package com.king.photo.activity;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
 
import com.king.photo.R;
import com.king.photo.util.Bimp;
import com.king.photo.util.FileUtils;
import com.king.photo.util.ImageItem;
import com.king.photo.util.PublicWay;
import com.king.photo.util.Res;
 
 
 
 //首页面activity
 
public class MainActivity extends Activity {
 
	private GridView noScrollgridview;
	private GridAdapter adapter;
	private View parentView;
	private PopupWindow pop = null;
	private LinearLayout ll_popup;
	public static Bitmap bimap ;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Res.init(this);
		bimap = BitmapFactory.decodeResource(
				getResources(),
				R.drawable.icon_addpic_unfocused);
		PublicWay.activityList.add(this);
		parentView = getLayoutInflater().inflate(R.layout.activity_selectimg, null);
		setContentView(parentView);
		Init();
	}
 
	public void Init() {
		
		pop = new PopupWindow(MainActivity.this);
		
		View view = getLayoutInflater().inflate(R.layout.item_popupwindows, null);
 
		ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
		
		pop.setWidth(LayoutParams.MATCH_PARENT);
		pop.setHeight(LayoutParams.WRAP_CONTENT);
		pop.setBackgroundDrawable(new BitmapDrawable());
		pop.setFocusable(true);
		pop.setOutsideTouchable(true);
		pop.setContentView(view);
		
		RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);
		Button bt1 = (Button) view
				.findViewById(R.id.item_popupwindows_camera);
		Button bt2 = (Button) view
				.findViewById(R.id.item_popupwindows_Photo);
		Button bt3 = (Button) view
				.findViewById(R.id.item_popupwindows_cancel);
		parent.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pop.dismiss();
				ll_popup.clearAnimation();
			}
		});
		bt1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				photo();
				pop.dismiss();
				ll_popup.clearAnimation();
			}
		});
		bt2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						AlbumActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
				pop.dismiss();
				ll_popup.clearAnimation();
			}
		});
		bt3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				pop.dismiss();
				ll_popup.clearAnimation();
			}
		});
		
		noScrollgridview = (GridView) findViewById(R.id.noScrollgridview);	
		noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
		adapter = new GridAdapter(this);
		adapter.update();
		noScrollgridview.setAdapter(adapter);
		noScrollgridview.setOnItemClickListener(new OnItemClickListener() {
 
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == Bimp.tempSelectBitmap.size()) {
					Log.i("ddddddd", "----------");
					ll_popup.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.activity_translate_in));
					pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
				} else {
					Intent intent = new Intent(MainActivity.this,
							GalleryActivity.class);
					intent.putExtra("position", "1");
					intent.putExtra("ID", arg2);
					startActivity(intent);
				}
			}
		});
 
	}
 
	@SuppressLint("HandlerLeak")
	public class GridAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private int selectedPosition = -1;
		private boolean shape;
 
		public boolean isShape() {
			return shape;
		}
 
		public void setShape(boolean shape) {
			this.shape = shape;
		}
 
		public GridAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}
 
		public void update() {
			loading();
		}
 
		public int getCount() {
			if(Bimp.tempSelectBitmap.size() == 9){
				return 9;
			}
			return (Bimp.tempSelectBitmap.size() + 1);
		}
 
		public Object getItem(int arg0) {
			return null;
		}
 
		public long getItemId(int arg0) {
			return 0;
		}
 
		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}
 
		public int getSelectedPosition() {
			return selectedPosition;
		}
 
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_published_grida,
						parent, false);
				holder = new ViewHolder();
				holder.image = (ImageView) convertView
						.findViewById(R.id.item_grida_image);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
 
			if (position ==Bimp.tempSelectBitmap.size()) {
				holder.image.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.icon_addpic_unfocused));
				if (position == 9) {
					holder.image.setVisibility(View.GONE);
				}
			} else {
				holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
			}
 
			return convertView;
		}
 
		public class ViewHolder {
			public ImageView image;
		}
 
		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					adapter.notifyDataSetChanged();
					break;
				}
				super.handleMessage(msg);
			}
		};
 
		public void loading() {
			new Thread(new Runnable() {
				public void run() {
					while (true) {
						if (Bimp.max == Bimp.tempSelectBitmap.size()) {
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
							break;
						} else {
							Bimp.max += 1;
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
						}
					}
				}
			}).start();
		}
	}
 
	public String getString(String s) {
		String path = null;
		if (s == null)
			return "";
		for (int i = s.length() - 1; i > 0; i++) {
			s.charAt(i);
		}
		return path;
	}
 
	protected void onRestart() {
		adapter.update();
		super.onRestart();
	}
 
	private static final int TAKE_PICTURE = 0x000001;
 
	public void photo() {
		Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(openCameraIntent, TAKE_PICTURE);
	}
 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case TAKE_PICTURE:
			if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {
				
				String fileName = String.valueOf(System.currentTimeMillis());
				Bitmap bm = (Bitmap) data.getExtras().get("data");
				FileUtils.saveBitmap(bm, fileName);
				
				ImageItem takePhoto = new ImageItem();
				takePhoto.setBitmap(bm);
				Bimp.tempSelectBitmap.add(takePhoto);
			}
			break;
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			for(int i=0;i<PublicWay.activityList.size();i++){
				if (null != PublicWay.activityList.get(i)) {
					PublicWay.activityList.get(i).finish();
				}
			}
			System.exit(0);
		}
		return true;
	}
 
}
 
 

下载地址 : 微信上传图片源码

备份下载地址:http://download.csdn.net/detail/jdsjlzx/8486449


补充:

通过GridView仿微信动态添加本地图片

此篇文章主要讲述GridView控件实现添加本地图片并显示.主要是关于GridView控件的基本操作,通常可以通过自定义继承BaseAdapter的适配器加载图片,而下面讲述的不是自定义的适配器,而是调用SimpleAdapter实现的.至于上传发布与网络交互此处不讲述,后面文章会讲!

一. 实现效果

    主要是通过点击+从本地相册中添加图片,同时显示图片至GridView.点击图片可以进行删除操作,同时界面中的发布EditView控件也很好看,不足之处在于+好没有移动至最后,但原理相同.
      

二. 项目工程结构




三. 界面布局详细代码

    1.主界面activity_main.xml
    主要通过相对布局实现,第一部分是底部的TextView,中间是EditView和GridView相对布局,下面是两个按钮.同时EditView调用res/drawable-hdpi中的editview_shape.xml,GridView显示的每张图片通过griditem_addpic.xml实现.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/container"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context="com.example.suishoupaipublish.MainActivity"  
  7.     tools:ignore="MergeRootFrame" >  
  8.       
  9.     <!-- 顶部添加文字 -->   
  10.     <RelativeLayout    
  11.         android:id="@+id/Layout_top"    
  12.         android:orientation="horizontal"     
  13.         android:layout_width="fill_parent"    
  14.         android:layout_height="40dp"    
  15.         android:layout_marginTop="5dp"  
  16.         android:layout_alignParentTop="true"    
  17.         android:gravity="center">    
  18.         <TextView    
  19.             android:layout_width="fill_parent"     
  20.             android:layout_height="wrap_content"      
  21.             android:textSize="25sp"  
  22.             android:gravity="center"  
  23.             android:text="发布信息" />   
  24.     </RelativeLayout>    
  25.     <!-- 底部按钮 -->    
  26.     <RelativeLayout    
  27.         android:id="@+id/Layout_bottom"     
  28.         android:layout_alignParentBottom="true"  
  29.         android:layout_width="fill_parent"     
  30.         android:layout_height="50dp"  
  31.         android:gravity="center" >    
  32.         <Button    
  33.             android:id="@+id/button1"    
  34.             android:layout_width="wrap_content"    
  35.             android:layout_height="fill_parent"  
  36.             android:textSize="20sp"  
  37.             android:text="发布拍拍" />    
  38.         <Button    
  39.             android:id="@+id/button2"    
  40.             android:layout_width="wrap_content"    
  41.             android:layout_height="fill_parent"  
  42.             android:layout_toRightOf="@+id/button1"  
  43.             android:textSize="20sp"  
  44.             android:text="取消发布" />  
  45.     </RelativeLayout>    
  46.     <!-- 显示图片 -->    
  47.     <RelativeLayout    
  48.         android:id="@+id/Content_Layout"       
  49.         android:layout_width="fill_parent"     
  50.         android:layout_height="fill_parent"    
  51.         android:layout_above="@id/Layout_bottom"     
  52.         android:layout_below="@id/Layout_top"      
  53.         android:gravity="center">       
  54.         <LinearLayout     
  55.             android:layout_width="match_parent"    
  56.             android:layout_height="match_parent"    
  57.             android:orientation="vertical"  
  58.             android:layout_alignParentBottom="true" >   
  59.             <!-- 设置运行多行 设置圆角图形 黑色字体-->  
  60.             <EditText   
  61.                 android:id="@+id/editText1"  
  62.                 android:layout_height="120dp"  
  63.                 android:layout_width="fill_parent"  
  64.                 android:textColor="#000000"  
  65.                 android:layout_margin="12dp"  
  66.                 android:textSize="20sp"  
  67.                 android:hint="随手说出你此刻的心声..."  
  68.                 android:maxLength="500"  
  69.                 android:singleLine="false"  
  70.                 android:background="@drawable/editview_shape" />  
  71.             <!-- 网格显示图片 行列间距5dp 每列宽度90dp -->  
  72.             <GridView  
  73.                 android:id="@+id/gridView1"  
  74.                 android:layout_width="fill_parent"  
  75.                 android:layout_height="200dp"  
  76.                 android:layout_margin="10dp"  
  77.                 android:background="#EFDFDF"  
  78.                 android:horizontalSpacing="5dp"  
  79.                 android:verticalSpacing="5dp"  
  80.                 android:numColumns="4"  
  81.                 android:columnWidth="90dp"  
  82.                 android:stretchMode="columnWidth"  
  83.                 android:gravity="center" >  
  84.             </GridView>  
  85.             <TextView   
  86.                 android:layout_width="fill_parent"  
  87.                 android:layout_height="wrap_content"  
  88.                 android:text="(友情提示:只能添加9张图片,长按图片可以删除已添加图片)"  
  89.                 android:gravity="center" />  
  90.         </LinearLayout>  
  91.     </RelativeLayout>  
  92.       
  93. </RelativeLayout>  
    2.显示ImageView图片布局griditem_addpic.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml 
    版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
    原文链接:https://blog.csdn.net/weixin_34087301/article/details/90685095
    站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
    • 发表于 2020-06-28 03:54:20
    • 阅读 ( 875 )
    • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢