import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.RotateAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;public class RotateBitmapActivity extends Activity {Button btnRotate1, btnRotate2;ImageView ivBitmap;//逆时针转45度RotateAnimation rotateAnimation = new RotateAnimation(0, -45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setTitle("旋转图片");setContentView(R.layout.rorate_bitmap);btnRotate1 = (Button) findViewById(R.id.btn_rorate);btnRotate2 = (Button) findViewById(R.id.btn_rorate_anim);ivBitmap = (ImageView) findViewById(R.id.iv_bitmap);        btnRotate1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubrotateBitmap1();}});        btnRotate2.setOnClickListener(new OnClickListener() {                @Override        public void onClick(View v) {        // TODO Auto-generated method stub        rotateBitmap2();        }        });}/** * 使用矩阵对象 旋转 */private void rotateBitmap1() {LinearLayout linLayout = new LinearLayout(this); BitmapDrawable bitmapDrawable = (BitmapDrawable) ivBitmap.getDrawable();//获取这个图片的宽和高        int width = bitmapDrawable.getBitmap().getWidth();         int height = bitmapDrawable.getBitmap().getHeight();         //定义预转换成的图片的宽度和高度        int newWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth() / 2;         int newHeight = getWindow().getWindowManager().getDefaultDisplay().getHeight() / 2;        //计算缩放率,新尺寸除原始尺寸        float scaleWidth = ((float) newWidth) / width;         float scaleHeight = ((float) newHeight) / height;         // 创建操作图片用的matrix对象        Matrix matrix = new Matrix();         // 缩放图片动作        matrix.postScale(scaleWidth, scaleHeight);         //旋转图片 动作        matrix.postRotate(90);         // 创建新的图片        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapDrawable.getBitmap(), 0, 0, width, height, matrix, true);                ivBitmap.setImageBitmap(resizedBitmap);}/** * 通过动画方式旋转,实际位置没有改变,当再次运行该函数,图像还是从原来的位置开始转动 */private void rotateBitmap2() { rotateAnimation.setDuration(1000);ivBitmap.startAnimation(rotateAnimation);rotateAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubrotateAnimation.setFillAfter(true);//动画后的效果固定下来,实际图片位置没有改变//逆时针转45度  坐标点算不好,//ivBitmap.layout(l, t, r, b);}});}}

更多相关文章

  1. Android(安卓)图片加载缓存
  2. 图片压缩
  3. Android(Java):按钮复选框点中效果
  4. Android使用libjpeg实现图片压缩
  5. android访问网络,下载图片,并设置UI
  6. Android(安卓)Retrofit与Spring后台配合,实现单张、多张图片上传
  7. Android(安卓)强大的图片加载缓存— Glide
  8. 让 Android(安卓)可以识别BMP图片文件,且目前Android所支持的所
  9. Android三种方法设置ImageView的图片

随机推荐

  1. Android闪屏示例程序
  2. android studio中的build.gradle文件
  3. Android(安卓)sdk 3.2蓝牙主动发起配对实
  4. Android(安卓)Dialog使用
  5. android 修改系统的dialog样式
  6. Android(安卓)网络连接——WebView
  7. 移动支付-修复FireFox在android移动设备
  8. android分层学习笔记(二)
  9. Android开发:什么是IBinder
  10. Android(安卓)中自定义View的应用(三)