Android 新增多張圖片。
阿新 • • 發佈:2019-01-10
先看效果圖。
直接上程式碼。主要是Adapter這邊。
public class GvAdapter extends BaseAdapter{
private Context context;
private int mMaxPosition;//根據這個list.size+1 來進行判斷
private List<String> list;
public GvAdapter(Context context, List<String> list) {
this.context = context;
this .list = list;
}
@Override
public int getCount() {
mMaxPosition=list.size()+1;
return mMaxPosition;
}
//根據情況可以設定是否需要。。
public int getMaxPosition(){
return mMaxPosition;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View v, ViewGroup
parent) {
ViewHolder vh=null;
if (v==null){
vh=new ViewHolder();
v= LayoutInflater.from(context).inflate(R.layout.item_gd,parent,false );
vh.img= (ImageView) v.findViewById(R.id.img);
vh.demimg= (ImageView) v.findViewById(R.id.delimg);
v.setTag(vh);
}else{
vh= (ViewHolder) v.getTag();
}
if (position==mMaxPosition-1){ //說明要顯示
Glide.with(context).load(R.drawable.id_photo).dontAnimate()
.centerCrop().into(vh.img);
vh.img.setVisibility(View.VISIBLE);
vh.demimg.setVisibility(View.GONE);
if (position==6&&mMaxPosition==7){//設定最大6個。那麼達到最大,就隱藏。
// vh.img.setImageResource(R.drawable.id_photo);
vh.img.setVisibility(View.GONE);
}
}else{//設定圖片。
vh.demimg.setVisibility(View.VISIBLE);
Glide.with(context).load(list.get(position)).into(vh.img);//設定
}
//刪除
vh.demimg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
});
return v;
}
public class ViewHolder{
public ImageView img,demimg;
}
//這裡引用了一個很好用的相簿框架
//相簿
compile 'com.linchaolong.android:imagepicker:1.2'
//載入
compile 'com.github.bumptech.glide:glide:3.8.0'
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private GvAdapter adapter;
private List<String> list;
ImagePicker imagePicker ;
String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.grid_view);
initView();
}
private void initView() {
imagePicker = new ImagePicker();
// 設定標題
imagePicker.setTitle("設定頭像");
// 設定是否裁剪圖片
imagePicker.setCropImage(true);
list = new ArrayList<>();
adapter = new GvAdapter(this, list);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View
view, int position, long id) {
//判斷是否是最後一個。
if (position==parent.getChildCount()-1){
if (position==6){//不能點選了
}else{
opnePhoto();
}
}else{//可以加點預覽功能。
}
}
});
}
public void opnePhoto() {
// 啟動圖片選擇器
imagePicker.startChooser(this, new ImagePicker.Callback() {
// 選擇圖片回撥
@Override
public void onPickImage(Uri imageUri) {
}
// 裁剪圖片回撥
@Override
public void onCropImage(Uri imageUri) {
if (list.size()>=6){
Toast.makeText(MainActivity.this,"最多選擇六張圖片",Toast.LENGTH_LONG).show();
}else{
list.add(String.valueOf(imageUri));
}
adapter.notifyDataSetChanged();
// path= String.valueOf(imageUri);
}
// 自定義裁剪配置
@Override
public void cropConfig(CropImage.ActivityBuilder
builder) {
builder
// 是否啟動多點觸控
.setMultiTouchEnabled(false)
// 設定網格顯示模式
.setGuidelines(CropImageView.Guidelines.OFF)
// 圓形/矩形
.setCropShape(CropImageView.CropShape
.RECTANGLE)
// 調整裁剪後的圖片最終大小
.setRequestedSize(960, 540)
// 寬高比
.setAspectRatio(16, 9);
}
// 使用者拒絕授權回撥
@Override
public void onPermissionDenied(int requestCode,
String[] permissions,
int[] grantResults) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
imagePicker.onActivityResult(MainActivity.this,requestCode, resultCode, data);
}
}
這是xml介面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="theagent.com.photo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<GridView
android:id="@+id/grid_view"
android:numColumns="4"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
</LinearLayout>