`

2011.10.14——— android 仿照微信的图片展示功能 之 基本功能

阅读更多
2011.10.14——— android 仿照微信的图片展示功能 之 基本功能

参考:http://blog.csdn.net/ameyume/article/details/6089334

先看一下微信的效果

显示




拖动



旋转



无限缩小



无限放大



这个就是微信里面图片的基本功能了



通过http://topic.csdn.net/u/20100810/15/5bb5a716-5260-415d-9db4-944a44e551cd.html我们知道 放大 缩小 的做法有两种

1、Matrix
2、动态的更改ImageView的宽和高


在这里 我们先用第一种方法 Matrix试试



package com.lp.image;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity implements OnTouchListener{
	private static final String TAG = "lp";
	
	
	/* 相关变量声明 */
	private ImageView mImageView;
	private Button zoomin;
	private Button zoomout;
	private Button rotate;
	private LinearLayout layoutImage;
	private LinearLayout zoomControll;
	//是否显示
	private boolean isVisible = true;
	private Bitmap bmp;
	private int id=0;
	private int displayWidth;
	private int displayHeight;
	private float scaleWidth=1;
	private float scaleHeight=1;
	private int rotateCount = 1;
	private float degrees=90;
	//上一个ImageView
	private ImageView lastImageView;
	
	private LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)    {
		super.onCreate(savedInstanceState);
		/* 加载display.xml Layout */
		setContentView(R.layout.main2);
		
		/* 取得屏幕分辨率大小 */
		DisplayMetrics dm=new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		displayWidth=dm.widthPixels;
		displayHeight=dm.heightPixels; 
		
		/* 初始化相关变量 */
//		Bundle bundle = this.getIntent().getExtras();
//		Integer imageId = bundle.getInt("imageId");
//		Log.i(TAG, "onCreate, imageId = " + imageId);
              	     
		bmp=BitmapFactory.decodeResource(getResources(), R.drawable.img); 
		mImageView = (ImageView)findViewById(R.id.myImageView);
		mImageView.setImageBitmap(bmp);
		mImageView.setOnTouchListener(this);
		
		layoutImage = (LinearLayout)findViewById(R.id.layoutImage);
		zoomin = (Button)findViewById(R.id.zoomin);
		zoomout = (Button)findViewById(R.id.zoomout); 
		rotate = (Button)findViewById(R.id.rotate);
		
		/* 缩小按钮onClickListener */
		zoomin.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				small(); 
			}
		});
		
		/* 放大按钮onClickListener */
		zoomout.setOnClickListener(new Button.OnClickListener() {
			@Override       
			public void onClick(View v) {
				big();
			} 
		});
		
		rotate.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				rotate();
			}
			
		});
		
		
		zoomControll = (LinearLayout)findViewById(R.id.zoomcontroll);
		
	}  
	

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onTouch...");
		if(isVisible){
			zoomControll.setVisibility(View.INVISIBLE);
			isVisible = false;
		}else{
			zoomControll.setVisibility(View.VISIBLE);
			isVisible = true;
		}
		return  super.onTouchEvent(event);    
	}

	
	/* 图片缩小的method */
	private void small()    {
		int bmpWidth=bmp.getWidth(); 
		int bmpHeight=bmp.getHeight();
		
		Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
		
		/* 设置图片缩小的比例 */
		double scale=0.8;
		/* 计算出这次要缩小的比例 */ 
		scaleWidth=(float) (scaleWidth*scale); 
		scaleHeight=(float) (scaleHeight*scale); 
		/* 产生reSize后的Bitmap对象 */
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, 
				bmpHeight,matrix,true); 
		
		if(id==0)      {
			/* 如果是第一次按,就删除原来默认的ImageView */
			layoutImage.removeView(mImageView);
		} else {
			/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
			layoutImage.removeView((ImageView)findViewById(id));
		} 
		
		/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
		id++;
		ImageView imageView = new ImageView(this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		imageView.setOnTouchListener(this);
		
		layoutImage.addView(imageView,params);
		Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()
				+ ", imageView.getHeight() = " + imageView.getHeight());
		/* 因为图片放到最大时放大按钮会disable,所以在缩小时把它重设为enable */ 
		zoomout.setEnabled(true);
	}
	
	/* 图片放大的method */
	private void big() {
		int bmpWidth=bmp.getWidth();
		int bmpHeight=bmp.getHeight();
		
		Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
		
		/* 设置图片放大的比例 */
		double scale=1.25;
		/* 计算这次要放大的比例 */
		scaleWidth=(float)(scaleWidth*scale);
		scaleHeight=(float)(scaleHeight*scale);
		/* 产生reSize后的Bitmap对象 */
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, 
				bmpHeight,matrix,true);
		
		if(id==0) {
			/* 如果是第一次按,就删除原来设置的ImageView */
			layoutImage.removeView(mImageView);
		} else {
			/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */ 
			layoutImage.removeView((ImageView)findViewById(id));
		}
		
		/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
		id++;
		ImageView imageView = new ImageView(this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		imageView.setOnTouchListener(this);
		layoutImage.addView(imageView,params);
		/* 如果再放大会超过屏幕大小,就把Button disable */
//		if( scaleWidth * scale * bmpWidth > bmpWidth * 3 ||
//			scaleHeight * scale * bmpHeight > bmpWidth * 3 ||
//			scaleWidth * scale * bmpWidth > displayWidth * 5 ||
//			scaleHeight * scale * bmpHeight > displayHeight * 5) {
//				zoomout.setEnabled(false);
//			} else {
//				zoomout.setEnabled(true);
//			}
	} 
	private void rotate() {
		int bmpWidth=bmp.getWidth(); 
		int bmpHeight=bmp.getHeight();
		
		Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
		
		/* 设置图片缩小的比例 */
		/* 产生reSize后的Bitmap对象 */
		if(rotateCount>4){
			rotateCount = 1;
		}
		float rotateDegrees = degrees * rotateCount;
		
		Matrix matrix = new Matrix();
		matrix.postRotate(rotateDegrees);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, 
				bmpHeight,matrix,true); 
		
		if(id==0)      {
			/* 如果是第一次按,就删除原来默认的ImageView */
			layoutImage.removeView(mImageView);
		} else {
			/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
			layoutImage.removeView((ImageView)findViewById(id));
		} 
		
		/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
		id++;
		ImageView imageView = new ImageView(this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		imageView.setOnTouchListener(this);
		
		layoutImage.addView(imageView,params);
		Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()
				+ ", imageView.getHeight() = " + imageView.getHeight());
		
		rotateCount++;
	}
}



xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
	>

<FrameLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:layout_gravity="center"
    >

	    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		    android:orientation="vertical"
		    android:layout_width="fill_parent"
		    android:layout_height="fill_parent"
		    android:id="@+id/layoutImage"
		    android:gravity="center"
		    >
		    <ImageView
		    	android:id="@+id/myImageView"
		    	android:layout_width="fill_parent"
		    	android:layout_height="fill_parent"
				/>
		</LinearLayout>
		<LinearLayout
			android:orientation="horizontal"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:gravity="bottom|right"
			android:layout_gravity="bottom"
			android:padding="10dip"
			android:id="@+id/zoomcontroll"
			>
			
			<Button 
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:background="@drawable/my_rotate_btn"
				android:id="@+id/rotate"
				/>
			<View
				android:layout_width="0dip"
				android:layout_height="wrap_content"
				android:layout_weight="1"
				/>
			<Button 
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:background="@drawable/my_zoomin_btn"
				android:id="@+id/zoomin"
				/>
			<View
				android:layout_width="2dip"
				android:layout_height="wrap_content"
				/>	
			<Button 
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:background="@drawable/my_zoomout_btn"
				android:id="@+id/zoomout"
				/>
				
		</LinearLayout>
</FrameLayout>

</LinearLayout>


这个xml捣鼓了半天 才弄出 不容易啊

效果如下:




可以放大 缩小 旋转

当然 这里面还有以下几个问题 没有解决:

1、放大超过屏幕显示不出来
2、无限放大内存溢出
3、旋转 放大 缩小的复合变化
4、拖动








  • 大小: 59.2 KB
  • 大小: 39.1 KB
  • 大小: 39.4 KB
  • 大小: 23.7 KB
  • 大小: 73.9 KB
  • 大小: 25.5 KB
分享到:
评论
1 楼 mmm333zzz 2012-02-28  
可以发一份源码给我吗?  谢谢:  421828229@qq.com

相关推荐

    微信小程序——新浪读书(截图+源码).zip

    微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip ...

    android 调用微信扫一扫(调用微信.so实现)

    在Android平台上,调用微信扫一扫功能通常涉及到集成微信官方提供的SDK,这一过程主要依赖于微信的动态链接库(.so文件),使得应用能够调用到微信的扫码服务。下面将详细介绍如何实现这一功能。 首先,我们需要从...

    Android仿微信图片编辑涂鸦.zip

    在Android平台上,开发一款类似微信的图片编辑功能,特别是实现涂鸦功能,是许多开发者可能会遇到的任务。这个“Android仿微信图片编辑涂鸦.zip”压缩包包含了一个名为"Doodle-master"的项目,它是一个Android应用...

    微信抢红包Android APP.zip

    微信抢红包Android APP.zip微信抢红包Android APP.zip微信抢红包Android APP.zip 微信抢红包Android APP.zip微信抢红包Android APP.zip微信抢红包Android APP.zip 微信抢红包Android APP.zip微信抢红包Android APP....

    Android 仿微信选择图片,上传图片

    在Android开发中,微信作为一款流行的社交应用,其图片选择和上传功能被广泛借鉴和学习。本示例项目"Android 仿微信选择图片,上传图片"旨在帮助开发者实现类似的功能,让用户可以从手机相册中选取图片,并能上传到...

    数据库大作业-学校人事信息管理系统.zip

    数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip...

    (03-25)新媒体营销渠道——以“微信”为例.zip

    (03-25)新媒体营销渠道——以“微信”为例.zip

    python项目——Word助手.zip

    python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目...

    微信公众号运营方案商业计划书.doc (2).docx

    微信公众号运营方案商业计划书.doc (2).docx微信公众号运营方案商业计划书.doc (2).docx微信公众号运营方案商业计划书.doc (2).docx微信公众号运营方案商业计划书.doc (2).docx微信公众号运营方案商业计划书.doc (2)...

    python项目——RCQ读者书库.zip

    python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ...

    详解Android微信登录与分享

    Android 使用微信登录、分享功能 具体的文档详情微信官网上介绍(微信官网文档),本人直接按照项目部署步骤进行讲解: 第一步:申请你的AppID; 第二步:依赖 dependencies { compile '...

    基于Android的仿微信朋友圈的实现.app

    【标题】: "基于Android的仿微信朋友圈的实现" 是一款旨在模拟微信朋友圈核心功能的移动应用程序。这个APP旨在提供类似微信的用户体验,让用户能够发布动态、参与二级评论以及进行点赞操作。 【描述】: 这个应用的...

    C语言项目——企业员工管理系统.zip

    C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统....

    安卓微信相关相关-android仿微信朋友圈图片浏览其中有图片的异步加双击图片放缩点击图片退出当前界面横向滚动图片查看.rar

    这个压缩包文件“安卓微信相关相关-android仿微信朋友圈图片浏览其中有图片的异步加双击图片放缩点击图片退出当前界面横向滚动图片查看.rar”提供了实现类似微信朋友圈图片浏览功能的代码示例。以下将详细介绍其中...

    微信小程序实例开发——IT书单(截图+源码).zip

    微信小程序实例开发——IT书单(截图+源码).zip 微信小程序实例开发——IT书单(截图+源码).zip 微信小程序实例开发——IT书单(截图+源码).zip 微信小程序实例开发——IT书单(截图+源码).zip 微信小程序实例...

    Android 微信朋友圈图片拖拽功能Demo

    在Android开发中,微信朋友圈的图片拖拽功能是一项常见的交互设计,它提供了用户对图片进行自由移动和调整位置的能力,增强了用户体验。这个"Android微信朋友圈图片拖拽功能Demo"就是一个实现此类功能的示例项目,名...

    android studio 制作简单微信登录界面

    在Android Studio中制作一个简单的微信登录界面涉及到多个技术层面,包括UI设计、微信SDK集成、权限申请以及网络请求处理等。下面将详细讲解这个过程。 首先,我们需要了解Android Studio IDE的基本操作。Android ...

    基于互联网思维的新媒体营销模式研究——以微信公众号为例.pdf

    基于互联网思维的新媒体营销模式研究——以微信公众号为例.pdf

    基于Android Studio开发的安卓的高仿微信

    8. **多媒体支持**:朋友圈功能涉及到图片和视频的上传与浏览,Android提供了`MediaStore`接口访问本地媒体库,`Camera` API用于拍照,`ExoPlayer`或`MediaPlayer`用于播放音频和视频。 9. **权限管理**:根据...

    2022年轻人微信情商报告.pdf

    2022年轻人微信情商报告.pdf 2022年轻人微信情商报告.pdf 2022年轻人微信情商报告.pdf 2022年轻人微信情商报告.pdf 2022年轻人微信情商报告.pdf 2022年轻人微信情商报告.pdf 2022年轻人微信情商报告.pdf 2022年轻人...

Global site tag (gtag.js) - Google Analytics