自定义View实现图片的绘制、旋转、缩放
2015-04-13 14:40
316 查看
1、图片
把一张JPG图片改名为image.jpg,然后拷贝到项目的res-drawable中。
2、activity_main.xml
把一张JPG图片改名为image.jpg,然后拷贝到项目的res-drawable中。
2、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/buttonLeft" android:text="图片向左移动" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonRight" android:text="图片向右移动" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/buttonRotationLeft" android:text="图片向左旋转" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonRotationRight" android:text="图片向右旋转" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonNarrow" android:text="图片缩小" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonEnlarge" android:text="图片放大" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.content.Context; import android.graphics.Paint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.Canvas; import android.widget.LinearLayout; import android.widget.Button; public class MainActivity extends Activity { ImageView imageView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 动态加载图片到LinearLayout中 imageView = new ImageView(this); LinearLayout ll = (LinearLayout) findViewById(R.id.imageid); ll.addView(imageView); // 向左移动 Button btnLeft = (Button) findViewById(R.id.buttonLeft); btnLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setPosLeft(); } }); // 向右移动 Button btnRight = (Button) findViewById(R.id.buttonRight); btnRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setPosRight(); } }); // 向左旋转 Button btnRotationLeft = (Button)findViewById(R.id.buttonRotationLeft); btnRotationLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setRotationLeft(); } }); // 向右旋转 Button btnRotationRight = (Button)findViewById(R.id.buttonRotationRight); btnRotationRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setRotationRight(); } }); // 放大图片 Button btnEnlarge = (Button)findViewById(R.id.buttonEnlarge); btnEnlarge.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setEnlarge(); } }); // 缩小图片 Button btnNarrow = (Button)findViewById(R.id.buttonNarrow); btnNarrow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageView.setNarrow(); } }); } // 自定义图片View class ImageView extends View { private Paint paint = null; // 画笔 private Bitmap bitmap = null; // 图片位图 private Bitmap bitmapDisplay = null; private Matrix matrix = null; private int nBitmapWidth = 0; // 图片的宽度 private int nBitmapHeight = 0; // 图片的高度 private int nPosX = 120; // 图片所在的位置X private int nPosY = 10; // 图片所在的位置Y private float fAngle = 0.0f; // 图片旋转 private float fScale = 1.0f; // 图片缩放 1.0表示为原图 public ImageView(Context context) { super(context); paint = new Paint(); paint.setFlags(Paint.ANTI_ALIAS_FLAG); // 加载需要操作的图片 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); bitmapDisplay = bitmap; matrix = new Matrix(); // 获取图片高度和宽度 nBitmapWidth = bitmap.getWidth(); nBitmapHeight = bitmap.getHeight(); } // 向左移动 public void setPosLeft() { nPosX -= 10; } // 向右移动 public void setPosRight() { nPosX += 10; } // 向左旋转 public void setRotationLeft() { fAngle--; setAngle(); } // 向右旋转 public void setRotationRight() { fAngle++; setAngle(); } // 图片放大 public void setEnlarge() { if (fScale < 2) { fScale += 0.1f; setScale(); } } // 图片缩小 public void setNarrow() { if (fScale > 0.5) { fScale -= 0.1f; setScale(); } } // 设置旋转比例 private void setAngle() { matrix.reset(); matrix.setRotate(fAngle); bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true); } // 设置缩放比例 private void setScale() { matrix.reset(); matrix.postScale(fScale, fScale); bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(bitmapDisplay, nPosX, nPosY, paint); invalidate(); } } }
相关文章推荐
- android自定义View实现图片的绘制、旋转、缩放
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果
- ImageView里面的图片实现同时移动缩放旋转 photoView自定义
- 【Android】自定义ImageView实现图片的平移、缩放、旋转(手势操作)
- 自定义View绘制圆形实现触发事件(单指移动,多指缩放)
- Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
- android实现自定义RelativeLayout可拖动、缩放、旋转TextView-更新版
- ViewPager中实现对图片的单指拖动缩放旋转
- 自定义View实现图片的拖动和缩放
- Android-图片预览(自定义ImageView实现图片缩放,多点触控,自由移动)
- UIImageView实现图片移动,缩放、旋转的代码片段
- android 自定义ImageView实现图片缩放边界回弹和缩小回弹
- 自定义View实现图片的拖动和缩放
- Android自定义View实现旋转的圆形图片
- Android自定义View实现不断旋转的圆形图片
- Android--自定义View加载网络图片,实现缩放,圆形剪裁
- 自定义View实现仿朋友圈的图片查看器,缩放、双击、移动、回弹、下滑退出及动画等
- Android通过自定义ImageView控件实现图片的缩放和拖动的实现代码
- 【Android 开发】:UI控件之 ImageView 实现图片旋转和缩放功能
- 自定义ImageView来实现图片旋转