|
|
@@ -0,0 +1,266 @@
|
|
|
+package com.usai.redant.raimage.PhotoList;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
+import android.support.v7.app.ActionBar;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.v7.widget.DefaultItemAnimator;
|
|
|
+import android.support.v7.widget.GridLayoutManager;
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.AbsListView;
|
|
|
+import android.widget.BaseAdapter;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.GridView;
|
|
|
+import android.widget.ImageButton;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.RelativeLayout;
|
|
|
+import android.widget.SimpleAdapter;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import com.usai.redant.raimage.R;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+
|
|
|
+public class PhotoGridActivity extends AppCompatActivity {
|
|
|
+
|
|
|
+ private GridView gridView;
|
|
|
+ private GridViewAdapter adapter;
|
|
|
+ private Button deleteBtn;
|
|
|
+ private ArrayList<HashMap<String,String>> photoDic;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_photo_grid);
|
|
|
+
|
|
|
+ Intent intent = getIntent();
|
|
|
+ ArrayList<String> photos = (ArrayList<String>) intent.getSerializableExtra("pic_list");
|
|
|
+ photoDic = new ArrayList<HashMap<String, String>>();
|
|
|
+ for (int i = 0; i < photos.size(); i++) { // 默认全部未选中
|
|
|
+
|
|
|
+ HashMap<String,String> dic = new HashMap<String, String>();
|
|
|
+ dic.put("path",photos.get(i));
|
|
|
+ dic.put("delete","0");
|
|
|
+
|
|
|
+ photoDic.add(dic);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ gridView = (GridView) findViewById(R.id.photo_grid_view);
|
|
|
+ deleteBtn = (Button)findViewById(R.id.grid_photo_delete_btn);
|
|
|
+
|
|
|
+ deleteBtn.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+
|
|
|
+ Iterator<HashMap<String,String>> iterator = photoDic.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ HashMap<String,String> item = iterator.next();
|
|
|
+ int delete = Integer.valueOf(item.get("delete"));
|
|
|
+ if (delete == 1) {
|
|
|
+ // 删除文件
|
|
|
+ new File(item.get("path")).delete();
|
|
|
+ iterator.remove();
|
|
|
+ int index = photoDic.indexOf(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ int h = getWindowManager().getDefaultDisplay().getHeight();
|
|
|
+ int w = getWindowManager().getDefaultDisplay().getWidth();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 设置Adapter
|
|
|
+ gridView.setAdapter(adapter = new GridViewAdapter());
|
|
|
+
|
|
|
+
|
|
|
+ /**Action Bar*/
|
|
|
+ ActionBar mActionBar = getSupportActionBar();
|
|
|
+ mActionBar.setHomeButtonEnabled(true);
|
|
|
+ mActionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
+ mActionBar.setTitle("RA Image");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
+ switch (item.getItemId()) {
|
|
|
+ case android.R.id.home: {
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showPhotoPreview(int index) {
|
|
|
+ ArrayList<String> photos = new ArrayList<String>();
|
|
|
+ Iterator<HashMap<String,String>> iterator = photoDic.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ HashMap<String,String> item = iterator.next();
|
|
|
+ photos.add(item.get("path"));
|
|
|
+ }
|
|
|
+// Toast.makeText(PhotoGridActivity.this,"Click" + index,Toast.LENGTH_LONG).show();
|
|
|
+ Intent intent = new Intent(this,NewPhotoPreviewActivity.class);
|
|
|
+ intent.putExtra("photos",(Serializable)photos);
|
|
|
+ intent.putExtra("index",index);
|
|
|
+ startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**缩小图片*/
|
|
|
+ public Bitmap scaleImage(String path,int width,int height) {
|
|
|
+ Bitmap source = BitmapFactory.decodeFile(path);
|
|
|
+
|
|
|
+// int originWidth = source.getWidth();
|
|
|
+// int originHeight = source.getHeight();
|
|
|
+//
|
|
|
+// int width = originWidth, height = originHeight;
|
|
|
+//
|
|
|
+// if (originHeight > 1024 || originWidth > 1024)
|
|
|
+// {
|
|
|
+// if (originWidth > originHeight)
|
|
|
+// {
|
|
|
+// width = 1024;
|
|
|
+// height = originHeight * 1024 / originWidth;
|
|
|
+//
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+//
|
|
|
+// height = 1024;
|
|
|
+// width = originWidth * 1024 / originHeight;
|
|
|
+//
|
|
|
+// }
|
|
|
+// }
|
|
|
+ Bitmap scaled = Bitmap.createScaledBitmap(source, width, height, true);
|
|
|
+ return scaled;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**Adapter*/
|
|
|
+ private class GridViewAdapter extends BaseAdapter {
|
|
|
+
|
|
|
+ private class PhotoViewHolder {
|
|
|
+ public ImageView photoView;
|
|
|
+ public ImageButton checkBtn;
|
|
|
+
|
|
|
+ public PhotoViewHolder(View view) {
|
|
|
+ photoView = (ImageView)view.findViewById(R.id.photo_list_iv);
|
|
|
+ checkBtn = (ImageButton)view.findViewById(R.id.photo_list_checkBtn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getCount() {
|
|
|
+ return photoDic.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getItem(int position) {
|
|
|
+ return position;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getItemId(int position) {
|
|
|
+ return position;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
+
|
|
|
+ PhotoViewHolder holder = null;
|
|
|
+ View cell;
|
|
|
+ if (convertView == null) {
|
|
|
+ cell = (View) LayoutInflater.from(PhotoGridActivity.this).inflate(R.layout.photo_list_item,null);
|
|
|
+ holder = new PhotoViewHolder(cell);
|
|
|
+ cell.setTag(holder);
|
|
|
+ } else {
|
|
|
+ cell = convertView;
|
|
|
+ holder = (PhotoViewHolder)cell.getTag();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ final HashMap<String,String> item = photoDic.get(position);
|
|
|
+ String path = item.get("path");
|
|
|
+
|
|
|
+
|
|
|
+ Bitmap bitmap = scaleImage(path,200,200);
|
|
|
+
|
|
|
+ holder.photoView.setImageBitmap(bitmap);
|
|
|
+
|
|
|
+ holder.checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_none));
|
|
|
+
|
|
|
+ int delete = Integer.valueOf(item.get("delete"));
|
|
|
+ if (delete == 1) {
|
|
|
+ holder.checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_check));
|
|
|
+ }
|
|
|
+
|
|
|
+ final int idx = position;
|
|
|
+ holder.checkBtn.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ ImageButton checkBtn = (ImageButton)v;
|
|
|
+ checkBtn.setSelected(!checkBtn.isSelected());
|
|
|
+ if (checkBtn.isSelected()) {
|
|
|
+ checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_check));
|
|
|
+ item.put("delete","1");
|
|
|
+ photoDic.set(idx,item);
|
|
|
+ } else {
|
|
|
+ checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_none));
|
|
|
+ item.put("delete","0");
|
|
|
+ photoDic.set(idx,item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ holder.photoView.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ showPhotoPreview(idx);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**解决Item被拉伸压缩*/
|
|
|
+ int itemWidth = (int)(getResources().getDisplayMetrics().widthPixels - 4 * 10) / 3;
|
|
|
+ int itemHeight = itemWidth;
|
|
|
+
|
|
|
+ AbsListView.LayoutParams param = (AbsListView.LayoutParams) cell.getLayoutParams();
|
|
|
+
|
|
|
+ if (param == null) {
|
|
|
+ /**解决第一个Item不响应*/
|
|
|
+ param = new AbsListView.LayoutParams(itemWidth, itemHeight);
|
|
|
+ } else {
|
|
|
+ param.width = itemWidth;
|
|
|
+ param.height = itemHeight;
|
|
|
+ }
|
|
|
+ cell.setLayoutParams(param);
|
|
|
+
|
|
|
+ return cell;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|