Android 旋转图片
发布时间:2020-12-14 23:26:23 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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,0.5f); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.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() { @Override public void onClick(View v) { // TODO Auto-generated method stub rotateBitmap1(); } }); 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(),width,height,matrix,true); ivBitmap.setImageBitmap(resizedBitmap); } /** * 通过动画方式旋转,实际位置没有改变,当再次运行该函数,图像还是从原来的位置开始转动 */ private void rotateBitmap2() { rotateAnimation.setDuration(1000); ivBitmap.startAnimation(rotateAnimation); rotateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub rotateAnimation.setFillAfter(true);//动画后的效果固定下来,实际图片位置没有改变 //逆时针转45度 坐标点算不好, // ivBitmap.layout(l,t,r,b); } }); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java indexOf()方法:返回第一次出现的索引位置
- java swing标准对话框具体实现
- java – 比较两个日期与时间,其中Formate是yyyy-MM-dd HH:
- java – Spring-Security 3 / Spring MVC和可怕的@Secured
- 如何用java创建一个“不要再问我”对话框弹出框?
- Android系统中发送短信和彩信的代码
- JavaFX:如何仅在中键上创建ScrollPane平移?
- spring boot 注入 property的三种方式(推荐)
- 压缩Java中的字节数组并在C中解压缩
- java – ExecutorService.Submit(Callable)为Future对象返回