关闭

Android仿微信照片浏览器

标签: android微信照片放大缩小照片浏览器
227人阅读 评论(0) 收藏 举报
分类:

Android仿微信照片浏览器

最近一直在忙IM,所以还给介绍一下IM相关的Android仿微信照片浏览器,支持照片放大缩小,长按、点击、点击照片内外等监听事件,可以实现市面上常见的长按保存照片的功能,点击照片和点击照片外的区域返回到上一界面。
效果图如下:IM聊天界面
照片查看
照片放大效果

上代码(介绍一个支持查看多张照片并且支持单张照片查看的)
xml:(HackyViewPager demo中有)

<?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:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/linearlayout_main"
    android:background="@color/black"
    >


    <com.motoband.ui.view.HackyViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        tools:context=".ui.activity.PicassoViewPagerActivity">

    </com.motoband.ui.view.HackyViewPager>
</LinearLayout>
public class PicassoViewPagerActivity extends AppCompatActivity {

    private ArrayList<String> mDatas = new ArrayList<String>();
    private int checkposition;
    HackyViewPager hackyViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picasso_viewpager);
        initView();
        initData();

    }

    private void initView() {

        hackyViewPager = (HackyViewPager) findViewById(R.id.view_pager);

    }

    private void initData() {

        //数据的传递
        mDatas = (ArrayList<String>) getIntent().getSerializableExtra("images");
        hackyViewPager.setAdapter(new SamplePagerAdapter());

        checkposition = getIntent().getIntExtra("position", 0) - 1;
        hackyViewPager.setCurrentItem(checkposition);

    }

    private class SamplePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (mDatas == null || mDatas.size() == 0) {
                return 0;
            }
            return mDatas.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, final int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
            Picasso.with(PicassoViewPagerActivity.this)
                    .load(mDatas.get(position))
                    .into(photoView, new Callback() {
                        @Override
                        public void onSuccess() {
                            attacher.update();
                        }

                        @Override
                        public void onError() {

                        }
                    });
            container.addView(photoView, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);


            //长按保存
            attacher.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    AlertDialog.Builder builder1 = new AlertDialog.Builder(PicassoViewPagerActivity.this);
                    builder1.setTitle("提示");
                    builder1.setMessage("确定要保存照片?");

                    builder1.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder1.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            String picPath = mDatas.get(position);
                            Bitmap bitmap = getHttpBitmap(picPath);
                            saveImageToGallery(getBaseContext(), bitmap);
                            dialog.dismiss();
                        }
                    });
                    builder1.show();

                    return false;
                }
            });

            //点击照片/非照片展示区域返回
            attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }

                @Override
                public void onOutsidePhotoTap() {
                    finish();
                }
            });


            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    //url转bitmap
    public Bitmap getHttpBitmap(String url) {
        Bitmap bitmap = null;
        try {
            URL pictureUrl = new URL(url);
            InputStream in = pictureUrl.openStream();
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }


    public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "Motoband");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Toast.makeText(context, "照片保存成功", Toast.LENGTH_SHORT).show();
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}

Android仿微信照片浏览器

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25206次
    • 积分:727
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:2篇
    • 译文:0篇
    • 评论:20条
    最新评论
    个人博客
    https://madreain.github.io/