前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2014-11-8Android学习------Android 实现图片的旋转--------动画Animation学习篇

2014-11-8Android学习------Android 实现图片的旋转--------动画Animation学习篇

作者头像
wust小吴
发布2022-03-07 14:19:29
3360
发布2022-03-07 14:19:29
举报
文章被收录于专栏:风吹杨柳风吹杨柳

我学习Android都是结合源代码去学习,这样比较直观,非常清楚的看清效果,觉得很好,今天的学习源码是网上找的源码 百度搜就知道很多下载的地方 网上源码的名字叫:Android 实现图片的旋转.zip

z直接看代码:

布局文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/myButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="逆时针"
            android:textSize="18sp" >
        </Button>

        <Button
            android:id="@+id/myButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顺时针"
            android:textSize="18sp" >
        </Button>
    </LinearLayout>

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/a" >
    </ImageView>

</LinearLayout>

效果图:

实现的activity类:

代码语言:javascript
复制
import com.wust.imgrotate.R;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class DemoActivity extends Activity {

	private ImageView mImageView;
	private Button btn1, btn2;
	private int ScaleTimes = 1, ScaleAngle = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mImageView = (ImageView) findViewById(R.id.myImageView);
		final Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.a);
		final int widthOrig = bmp.getWidth();
		final int heightOrig = bmp.getHeight();
		mImageView.setImageBitmap(bmp);
		btn1 = (Button) findViewById(R.id.myButton1);
		btn1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ScaleAngle--;
				if (ScaleAngle < -60) {
					ScaleAngle = -60;
				}
				int newWidth = widthOrig * ScaleTimes;
				int newHeight = heightOrig * ScaleTimes;
				float scaleWidth = ((float) newWidth) / widthOrig;
				float scaleHeight = ((float) newHeight) / heightOrig;
				Matrix matrix = new Matrix();
				matrix.postScale(scaleWidth, scaleHeight);
				matrix.setRotate(5 * ScaleAngle);
				Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, widthOrig,
						heightOrig, matrix, true);
				BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(
						resizeBitmap);
				mImageView.setImageDrawable(myNewBitmapDrawable);
			}
		});
		btn2 = (Button) findViewById(R.id.myButton2);
		btn2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ScaleAngle++;
				if (ScaleAngle > 60) {
					ScaleAngle = 60;
				}
				int newWidth = widthOrig * ScaleTimes;
				int newHeight = heightOrig * ScaleTimes;
				float scaleWidth = ((float) newWidth) / widthOrig;
				float scaleHeight = ((float) newHeight) / heightOrig;
				Matrix matrix = new Matrix();
				matrix.postScale(scaleWidth, scaleHeight);
				matrix.setRotate(5 * ScaleAngle);
				Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, widthOrig,
						heightOrig, matrix, true);
				BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(
						resizeBitmap);
				mImageView.setImageDrawable(myNewBitmapDrawable);
			}
		});
	}

}

运行的效果:

知识点学习:

1.

Matrix 是一个处理翻转、缩放等图像效果的重要类

Matrix.postScale 可设置缩放比例,默认为1

void

setRotate(float degrees):参数是一个度数,默认绕(0,0)转 Set the matrix to rotate about (0,0) by the specified number of degrees.

void

setRotate(float degrees, float px, float py):参数是一个度数,绕着点(x,y) Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

void

setScale(float sx, float sy, float px, float py) Set the matrix to scale by sx and sy, with a pivot point at (px, py).

void

setScale(float sx, float sy) Set the matrix to scale by sx and sy.

以点px,py为原点缩放 >=0 1为正常大小

如果是负数,图形就会翻转

如果没设置原点坐标,默认以0,0点缩放(如果发现图片不见了,检查一下是不是翻转出了屏幕)

更多关于Matrix的知识点请看我下篇转载的文章

2.

Bitmap.createBitmap(source, x, y, width, height, m, filter)

@param source The bitmap we are subsetting

* @param x The x coordinate of the first pixel in source

* @param y The y coordinate of the first pixel in source

* @param width The number of pixels in each row

* @param height The number of rows

* @param m Optional matrix to be applied to the pixels

* @param filter true if the source should be filtered.

* @return A bitmap that represents the specified subset of source

Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, widthOrig,heightOrig, matrix, true);

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档