android 仿微信朋友圈图片选择控件

发布时间:2017-7-1 11:49:58编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"android 仿微信朋友圈图片选择控件 ",主要涉及到android 仿微信朋友圈图片选择控件 方面的内容,对于android 仿微信朋友圈图片选择控件 感兴趣的同学可以参考一下。

调用方式(布局文件就是一个自定义控件):

private ArrayList<String> selectedImages;    @BindView(R.id.imagePicker)    ImagePicker imagePicker;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_9);        ButterKnife.bind(this);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        imagePicker.setOnActivityResult(requestCode, resultCode, data);        selectedImages = imagePicker.getSelectedPicture();    }

代码;(文件下载)

0.AndroidManifest.xml

 <activity android:name=".SelectPictureActivity"/>
    <uses-permission android:name="android.permission.INTERNET"/>    <uses-feature android:name="android.hardware.camera" />    <uses-feature android:name="android.hardware.camera.autofocus" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1.自定义控件和布局

package de.bvb.rxdemo.widget;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.graphics.BitmapFactory;import android.graphics.drawable.ColorDrawable;import android.os.Bundle;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.GridView;import android.widget.ImageView;import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.assist.QueueProcessingType;import java.util.ArrayList;import de.bvb.rxdemo.R;import de.bvb.rxdemo.SelectPictureActivity;import static android.app.Activity.RESULT_OK;/** * http://blog.csdn.net/u013059863/article/details/49708317 */public class ImagePicker extends GridView {    /** 最多可以选择的图片数量 */    public static final int MAX_SELECTION = 9;    private static final int REQUEST_PICK = 0;    private Activity activity;    private GridAdapter gridAdapter;    public ImagePicker(Context activity) {        this(activity, null);    }    public ImagePicker(Context activity, AttributeSet attrs) {        this(activity, attrs, 0);    }    public ImagePicker(Context activity, AttributeSet attrs, int defStyleAttr) {        super(activity, attrs, defStyleAttr);        this.activity = (Activity) activity;        this.setHorizontalSpacing(20);        this.setVerticalSpacing(20);        this.setNumColumns(3);        this.setSelector(new ColorDrawable(0x00000000));        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity)                .threadPriority(Thread.NORM_PRIORITY - 2)//设置当前线程的优先级                .denyCacheImageMultipleSizesInMemory()                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//使用MD5对UIL进行加密命名                .diskCacheSize(100 * 1024 * 1024)//50 Mb sd卡(本地)缓存的最大值                .diskCacheFileCount(300)// 可以缓存的文件数量                .tasksProcessingOrder(QueueProcessingType.LIFO)//后进先出                .build();        //初始化操作        ImageLoader.getInstance().init(config);        gridAdapter = new GridAdapter();        setAdapter(gridAdapter);    }    //存放所有选择的照片    private ArrayList<String> allSelectedPicture = new ArrayList<String>();//    //存放从选择界面选择的照片//    private ArrayList<String> selectedPicture = new ArrayList<String>();    /** 获取选择的图片 */    public ArrayList<String> getSelectedPicture() {        return allSelectedPicture;    }    /** 需要覆盖被调用的Activity的 OnActivityResult方法 */    public void setOnActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESULT_OK) {            ArrayList<String> selectedPicture = (ArrayList) data.getSerializableExtra(SelectPictureActivity.INTENT_SELECTED_PICTURE);            for (String str : selectedPicture) {                if (!allSelectedPicture.contains(str)) {                    allSelectedPicture.add(str);                    setAdapter(gridAdapter);                }            }        }    }    /** 展示图片的GridView的适配器 */    class GridAdapter extends BaseAdapter {        public LayoutInflater layoutInflater = LayoutInflater.from(activity);        @Override        public int getCount() {            return allSelectedPicture.size() + 1;        }        @Override        public Object getItem(int position) {            return position;        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(final int position, View convertView, ViewGroup parent) {            ViewHolder holder = null;            if (convertView == null) {                holder = new ViewHolder();                convertView = layoutInflater.inflate(R.layout.childgrid_item, null);                holder.image = (ImageView) convertView.findViewById(R.id.child_iv);                holder.btn = (Button) convertView.findViewById(R.id.child_delete);                holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);                convertView.setTag(holder);            } else {                holder = (ViewHolder) convertView.getTag();            }            if (position == allSelectedPicture.size()) {                holder.image.setImageBitmap(BitmapFactory.decodeResource(                        activity.getResources(), R.drawable.icon_addpic));                holder.btn.setVisibility(View.GONE);                holder.image.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        selectClick();                    }                });                if (position == MAX_SELECTION) {                    holder.image.setVisibility(View.GONE);                }            } else {                ImageLoader.getInstance().displayImage("file://" + allSelectedPicture.get(position),                        holder.image);                holder.btn.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        //点击后移除图片                        allSelectedPicture.remove(position);                        //更新UI                        setAdapter(gridAdapter);                    }                });            }            return convertView;        }        class ViewHolder {            public ImageView image;            public Button btn;        }    }    private void selectClick() {        Intent intent = new Intent();        intent.setClass(activity, SelectPictureActivity.class);        Bundle bundle = new Bundle();        bundle.putStringArrayList("allSelectedPicture", allSelectedPicture);        intent.putExtras(bundle);        if (allSelectedPicture.size() < MAX_SELECTION) {            activity.startActivityForResult(intent, REQUEST_PICK);        }    }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="120dp"                android:layout_height="120dp" >    <ImageView        android:id="@+id/child_iv"        android:layout_width="120dp"        android:layout_height="120dp"        android:scaleType="centerCrop"        android:src="@drawable/ic_launcher" />    <Button        android:id="@+id/child_delete"        android:layout_width="25dp"        android:layout_height="25dp"        android:layout_alignParentRight="true"        android:layout_margin="4dp"        android:background="@drawable/ic_delete" /></RelativeLayout>
View Code

2.选择图片页面  SelectPictureActivity.java

package de.bvb.rxdemo;import android.app.Activity;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.Window;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.TranslateAnimation;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.ListView;import android.widget.TextView;import android.widget.Toast;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.assist.ImageScaleType;import java.io.File;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;public class SelectPictureActivity extends Activity {    // 最多选择图片的个数    private static int MAX_NUM = 9;    private static final int TAKE_PICTURE = 520;    public static final String INTENT_MAX_NUM = "intent_max_num";    public static final String INTENT_SELECTED_PICTURE = "intent_selected_picture";    private Context context;    private GridView gridview;    private PictureAdapter adapter;    /**     * 临时的辅助类,用于防止同一个文件夹的多次扫描     */    private HashMap<String, Integer> tmpDir = new HashMap<String, Integer>();    private ArrayList<ImageFloder> mDirPaths = new ArrayList<ImageFloder>();    /**     * 具体下载图片,缓存图片,显示图片的具体执行类,它有两个具体的方法displayImage(...)、loadImage(...),     */    private ImageLoader loader;    /**     * 用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片,     * 是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理。     */    private DisplayImageOptions options;    private ContentResolver mContentResolver;    //选择按钮和完成按钮    private Button btn_select, btn_ok;    //图片文件的列表    private ListView listview;    private FolderAdapter folderAdapter;    private ImageFloder imageAll, currentImageFolder;    //已选择的图片    private ArrayList<String> selectedPicture = new ArrayList<String>();    private String cameraPath = null;    private int isExisted;    private ArrayList<String> existedPictureList = new ArrayList<String>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_select_picture);        MAX_NUM = getIntent().getIntExtra(INTENT_MAX_NUM, 9);        Bundle bundle = getIntent().getExtras();        existedPictureList = bundle.getStringArrayList("allSelectedPicture");        isExisted = existedPictureList.size();        context = this;        mContentResolver = getContentResolver();        loader = ImageLoader.getInstance();        options = new DisplayImageOptions.Builder()                .showImageOnLoading(R.drawable.ic_launcher)//设置图片在下载期间显示的图片                .showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片                .showImageOnFail(R.drawable.ic_launcher)//设置图片加载/解码过程中错误时候显示的图片                .cacheInMemory(true)//设置下载的图片是否缓存在内存中                .cacheOnDisk(true) //设置下载的图片是否缓存在SD卡中                .considerExifParams(true)//启用EXIF和JPEG图像格式                .imageScaleType(ImageScaleType.EXACTLY)//图像将完全按比例缩小的目标大小                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型                .build();        initView();    }    /**     * 点击"所有图片"按钮事件     *     * @param v     */    public void select(View v) {        if (listview.getVisibility() == View.VISIBLE) {            hideListAnimation();        } else {            listview.setVisibility(View.VISIBLE);            showListAnimation();            folderAdapter.notifyDataSetChanged();        }    }    /**     * 设置ListView的移动的动画效果     */    public void showListAnimation() {        TranslateAnimation ta = new TranslateAnimation(1, 0f, 1, 0f, 1, 1f, 1, 0f);        ta.setDuration(200);        listview.startAnimation(ta);    }    /**     * 隐藏ListView的移动的动画效果     */    public void hideListAnimation() {        TranslateAnimation ta = new TranslateAnimation(1, 0f, 1, 0f, 1, 0f, 1, 1f);        ta.setDuration(200);        listview.startAnimation(ta);        ta.setAnimationListener(new AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationRepeat(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                listview.setVisibility(View.INVISIBLE);            }


上一篇:Python shell对比
下一篇:Hadoop Hive基础sql语法

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款