网络图片查看器 ——图片可放大缩小

来源:互联网 发布:什么是双色球密码算法 编辑:程序博客网 时间: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

1UI线程尽量只做跟UI相关的工作

2:耗时的工作(比如数据库操作,I/O,连接网络或者别的有可能阻碍UI线程的操作)把它放入单独的线程处理

3:尽量用Handler来处理UIthread和别的thread之间的交互

UI线程主要包括如下:

  1. Activity:onCreate(), onResume(), onDestroy(), onKeyDown(), onClick(),etc

  2. AsyncTask: onPreExecute(), onProgressUpdate(), onPostExecute(), onCancel,etc

  3. Mainthread handler: handleMessage(), post*(runnable r), etc


0 0