网络图片查看器 ——图片可放大缩小
来源:互联网 发布:什么是双色球密码算法 编辑:程序博客网 时间:2024/05/26 07:27
任务要求
在文本框中输入图片的路径,点击浏览按钮的同时,将会在上方的ImageView中显示出来该图片。
UI设计
package snowfox.android;
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.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ImageView v1;
private int widthOrg, heightOrg, w;
float scale = 1;
float scaleWidth, scaleHeight;
Bitmap OrgMap, newMap;
BitmapDrawable bmd;
Matrix matrix = new Matrix();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v1 = (ImageView)findViewById(R.id.image01);
OrgMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
widthOrg = OrgMap.getWidth();
heightOrg = OrgMap.getHeight();
w = 320;
scale = (float)w/widthOrg;
matrix = new Matrix();
matrix.postScale(scale, scale);
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
if(scale < 2.0 && w < 1200)
{
scale += 0.1;
matrix.reset();
matrix.postScale(scale, scale); //设置矩阵属性
//重新绘图
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
//转化为drawable图像,使其可以显示在imageview中
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
w = newMap.getWidth();
int h = newMap.getHeight();
Log.i("ta==========", w + " " +h);
}
return true;
}
//缩小图片
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
//设置缩放尺寸,若小于0.1倍,则停止缩小
if(scale > 0.1)
{
scale -= 0.05;
matrix.reset(); //重置矩阵
matrix.postScale(scale, scale); //设置矩阵属性
//重新绘图
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
//转化为drawable图像,使其可以显示在imageview中
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
w = newMap.getWidth();
int h = newMap.getHeight();
Log.i("ta==========", w + " " +h);
}
return true;
}
else
return super.onKeyDown(keyCode, event); //在其他情况下才调用super是为了屏蔽音量按键原来的功能,同时保证其他按键原功能可用
}
}
xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:fillViewport = "true">
<LinearLayout
android:gravity ="center"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:id = "@+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType ="fitCenter"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
出现错误如何避免KeyDispatchTimeout
1:UI线程尽量只做跟UI相关的工作
2:耗时的工作(比如数据库操作,I/O,连接网络或者别的有可能阻碍UI线程的操作)把它放入单独的线程处理
3:尽量用Handler来处理UIthread和别的thread之间的交互
UI线程主要包括如下:
Activity:onCreate(), onResume(), onDestroy(), onKeyDown(), onClick(),etc
AsyncTask: onPreExecute(), onProgressUpdate(), onPostExecute(), onCancel,etc
Mainthread handler: handleMessage(), post*(runnable r), etc
- 网络图片查看器 ——图片可放大缩小
- Android图片查看器,点击可放大缩小,左右滑动
- 网络图片放大缩小
- 网络图片放大缩小
- Ionic 相册(可放大缩小图片)
- iOS图片查看滚动放大缩小
- ReactNative图片放大缩小查看笔记
- js图片拖拽查看并滑动鼠标可放大缩小
- 图片缩小放大脚本
- 图片放大缩小代码
- flex图片放大缩小
- java 图片放大缩小
- 图片放大缩小
- js图片放大缩小
- C# 图片放大、缩小
- js图片放大缩小
- Android 图片放大缩小
- ZoomControls放大缩小图片
- LeetCode Sqrt(x)
- MyEclipse2015+Tomcat+MAVEN+SVN项目环境搭建
- 质量管理:测试基础架构图
- linux内核之关于内存屏障
- Maven pom.xml配置详解
- 网络图片查看器 ——图片可放大缩小
- 重采样内插方法
- poj 2385 来回走接苹果dp
- Notification(通知栏)
- 各种排序算法之比较
- 运行效率啊-2分钟与475毫秒的对比
- 架构师知识体系(3)--系统架构管理实践
- Python学习笔记 - 列表生成式listComprehensions
- linux0.12之内存管理(1)-整体框架思路