实例介绍
【实例简介】
一款非常好的图片浏览器源码,功能很强大,双击可以实现放大,还有拖动功能
【实例截图】
【核心代码】
/*****************Copyright (C), 2010-2015, FORYOU Tech. Co., Ltd.********************/ package com.android.photostore; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.Bitmap.Config; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import com.android.photostore.zoom.ImageZoomView; import com.android.photostore.zoom.SimpleZoomListener; import com.android.photostore.zoom.ZoomState; import com.android.photostore.zoom.SimpleZoomListener.ControlType; /** * @Filename: ImageSwitcher.java * @Author: wanghb * @Email: wanghb@foryouge.com.cn * @CreateDate: 2011-7-15 * @Description: description of the new class * @Others: comments * @ModifyHistory: */ public class ImageSwitcher extends Activity { private int mIndex; private int mItemwidth; private int mItemHerght; private ArrayList<String> pathes; private ProgressBar mProgressBar; /** Image zoom view */ private ImageZoomView mZoomView; /** Zoom state */ private ZoomState mZoomState; /** On touch listener for zoom view */ private SimpleZoomListener mZoomListener; private Bitmap zoomBitmap; private ImageView mMovedItem; private boolean isMoved; private FlingGallery mFlingGallery; public int getmIndex() { return mIndex; } public void updateState(int visibility) { mProgressBar.setVisibility(visibility); mFlingGallery.setCanTouch(View.GONE == visibility); } private boolean isViewIntent() { String action = getIntent().getAction(); return Intent.ACTION_VIEW.equals(action); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mItemwidth = dm.widthPixels; mItemHerght = dm.heightPixels; // mInflater = LayoutInflater.from(this); if (!isViewIntent()) { pathes = intent.getStringArrayListExtra("pathes"); mIndex = intent.getIntExtra("index", 0); } else { pathes = new ArrayList<String>(); pathes.add(intent.getData().getPath()); mIndex = 0; } setContentView(R.layout.myhorizontalview); mProgressBar = (ProgressBar) findViewById(R.id.progress_circular); mMovedItem = (ImageView) findViewById(R.id.removed); mFlingGallery = (FlingGallery) findViewById(R.id.horizontalview); mZoomView = (ImageZoomView) findViewById(R.id.zoomview); mZoomState = new ZoomState(); mZoomListener = new SimpleZoomListener(); mZoomListener.setmGestureDetector(new GestureDetector(this, new MyGestureListener())); mZoomListener.setZoomState(mZoomState); mZoomListener.setControlType(ControlType.ZOOM); mZoomView.setZoomState(mZoomState); mZoomView.setOnTouchListener(mZoomListener); // hsv = (MyHorizontalScrollView) mFlingGallery.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, pathes) { @Override public View getView(int position, View convertView, ViewGroup parent) { return new GalleryViewItem(getApplicationContext(), position); } }, mIndex); } public void goneTempImage() { } private Bitmap getDrawable(int index, int zoom) { if (index >= 0 && index < pathes.size()) { String path = pathes.get(index); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int mWidth = options.outWidth; int mHeight = options.outHeight; int s = 1; while ((mWidth / s > mItemwidth * 2 * zoom) || (mHeight / s > mItemHerght * 2 * zoom)) { s *= 2; } options = new BitmapFactory.Options(); options.inPreferredConfig = Config.ARGB_8888; options.inSampleSize = s; Bitmap bm = BitmapFactory.decodeFile(path, options); if (bm != null) { int h = bm.getHeight(); int w = bm.getWidth(); float ft = (float) ((float) w / (float) h); float fs = (float) ((float) mItemwidth / (float) mItemHerght); int neww = ft >= fs ? mItemwidth * zoom : (int) (mItemHerght * zoom * ft); int newh = ft >= fs ? (int) (mItemwidth * zoom / ft) : mItemHerght * zoom; float scaleWidth = ((float) neww) / w; float scaleHeight = ((float) newh) / h; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true); return bm; } } return null; } private void resetZoomState() { int currentIndex = mFlingGallery.getCurrentPosition(); if (zoomBitmap != null) { zoomBitmap.recycle(); } zoomBitmap = getDrawable(currentIndex,3); mZoomView.setImage(zoomBitmap); mZoomListener.setControlType(ControlType.ZOOM); mZoomState.setPanX(0.5f); mZoomState.setPanY(0.5f); mZoomState.setZoom(3f); mZoomState.notifyObservers(); } /** * 查看模式 * * @Description: * @Author: wanghb * @Email: wanghb@foryouge.com.cn * @Others: */ public void goToZoomPage() { resetZoomState(); mFlingGallery.setVisibility(View.GONE); isMoved = false; mZoomView.setVisibility(View.VISIBLE); mMovedItem.setBackgroundColor(0x0000); mMovedItem.setVisibility(View.VISIBLE); } public void goToSwicherPage() { mMovedItem.setVisibility(View.GONE); mFlingGallery.setVisibility(View.VISIBLE); mZoomView.setVisibility(View.GONE); } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDoubleTap(MotionEvent e) { goToSwicherPage(); return true; } } public void movedClick(View v) { isMoved = !isMoved; if (isMoved) { mZoomListener.setControlType(ControlType.PAN); mMovedItem.setBackgroundColor(R.drawable.pressed_application_background); } else { mZoomListener.setControlType(ControlType.ZOOM); mMovedItem.setBackgroundColor(0x0000); } } @Override protected void onDestroy() { if (zoomBitmap != null) { zoomBitmap.recycle(); } super.onDestroy(); } private class GalleryViewItem extends LinearLayout { public GalleryViewItem(Context context, int position) { super(context); this.setOrientation(LinearLayout.VERTICAL); this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); ImageView iv = new ImageView(context); iv.setImageBitmap(getDrawable(position, 1)); this.addView(iv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); } } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论