写一篇文章很辛苦啊!!!

转载请注明,联系请邮件nlp30508@qq.com


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


z直接看代码:

布局文件:

<?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类:

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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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);}});}}

运行的效果:

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

知识点学习:

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);


更多相关文章

  1. Android最新源码4.3下载-教程 2013-11
  2. Android应用Preference相关及源码浅析(SharePreferences篇)
  3. android 视频播放器 android videoView 按不同比例缩放 .
  4. Android AlarmManager实现定时任务(也就是闹钟) 附Demo源码
  5. android中使用线程池和临时缓存优化网络图片加载
  6. Android Nine Patch图片及按钮背景
  7. Android属性动画ValueAnimator源码简单分析
  8. android-Handler源码解析

随机推荐

  1. 一次Mysql使用IN大数据量的优化记录
  2. 详解mysql慢日志查询
  3. mysql8.0.20配合binlog2sql的配置和简单
  4. MySQL索引失效的几种情况汇总
  5. 详解MySQL 聚簇索引与非聚簇索引
  6. MySQL 索引的优缺点以及创建索引的准则
  7. MySQL MyISAM 与InnoDB 的区别
  8. MySQL btree索引与hash索引区别
  9. mysql group by 对多个字段进行分组操作
  10. 基于JPQL实现纯SQL语句方法详解