ImageView实现图像的放大缩小和旋转功能

来源:互联网 发布:网络装修公司 编辑:程序博客网 时间:2024/05/21 17:33

上次看到了《老罗的视频开发教程》对ImageView实现图像的旋转和放缩功能,今天写下这个方法。

一)实现图像的放大缩小

       1)首先在xml文件中定义ImageView控件,内容如下:       

             <ImageView
                android:id="@+id/imageview"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:contentDescription="@string/app_name"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_activity" />

                <SeekBar       
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:id="@+id/seekbar1"
                 android:layout_marginTop="20dp"
                 />seekBar的作用是选择不同的位置,使图像相应的放大。

      2)然后在java代码中:

             DisplayMetrics dm=new DisplayMetrics();//这个类的作用是获取屏幕的像素
             getWindowManager().getDefaultDisplay().getMetrics(dm);
             final int width=dm.widthPixels;//屏幕的水平像素
             final int hei=dm.heightPixels;//屏幕的垂直像素
             seekBar.setOnSeekBarChangeListener(this);

             在seekBar的事件监听事件中:

             int newWidth=arg1+minWidth;//arg1表示seekbar的进度值
             int newHeight=(int)newWidth*3/4;//水平和垂直的高度比是4:3

             imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
             textView1.setText("图像宽度:"+newWidth+"图像高度:"+newHeight);

二)实现图像的旋转

        1)首先在xml文件中定义ImageView(上面那个)和一个SeekBar控制旋转的角度

                <SeekBar
                   android:layout_marginTop="20dp"
                   android:max="360"        //360度旋转
                   android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/seekbar2"
                  ></SeekBar>

          2)用代码去实现

                 Bitmap bp=((BitmapDrawable)(getResources().getDrawable(R.drawable.ic_activity))).getBitmap();//获取图片
                 matrix.setRotate(arg1);//设置角度
                 Bitmap bitmap=Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), matrix, true);//重绘图像
                 imageView.setImageBitmap(bitmap);//显示在ImageView中

注:把工程代码贴上来,地址如下:http://download.csdn.net/detail/yinhaojun891226/5319136
             

原创粉丝点击