Android實現(xiàn)Bitmap位圖旋轉(zhuǎn)效果
位圖的旋轉(zhuǎn)也可以借助Matrix或者Canvas來實現(xiàn)。
通過postRotate方法設(shè)置旋轉(zhuǎn)角度,然后用createBitmap方法創(chuàng)建一個經(jīng)過旋轉(zhuǎn)處理的Bitmap對象,最后用drawBitmap方法繪制到屏幕上,于是就實現(xiàn)了旋轉(zhuǎn)操作。
下面例子中把原位圖和經(jīng)旋轉(zhuǎn)處理的位圖都繪制到屏幕上,目的是做一個對比。

package xiaosi.bitmap;
import android.app.Activity;
import android.os.Bundle;
public class mianActivity extends Activity
{
private BitmapView bitmapView = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
bitmapView = new BitmapView(this);
setContentView(bitmapView);
}
}
BitmapView.Java
package xiaosi.bitmap;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.View;
public class BitmapView extends View
{
public BitmapView(Context context)
{
super(context);
}
//重寫onDraw方法
public void onDraw(Canvas canvas)
{
// 獲取資源文件的引用res
Resources res = getResources();
// 獲取圖形資源文件
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.h);
// 設(shè)置canvas畫布背景為白色
canvas.drawColor(Color.BLACK);
// 在畫布上繪制縮放之前的位圖,以做對比
//屏幕上的位置坐標(biāo)是0,0
canvas.drawBitmap(bmp, 0, 0, null);
// 定義矩陣對象
Matrix matrix = new Matrix();
// 縮放原圖
matrix.postScale(1f, 1f);
// 向左旋轉(zhuǎn)45度,參數(shù)為正則向右旋轉(zhuǎn)
matrix.postRotate(-45);
//bmp.getWidth(), 500分別表示重繪后的位圖寬高
Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), 500,
matrix, true);
// 在畫布上繪制旋轉(zhuǎn)后的位圖
//放在坐標(biāo)為0,200的位置
canvas.drawBitmap(dstbmp, 0, 200, null);
}
}
源代碼下載:點擊打開鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android中Bitmap用法(顯示,保存,縮放,旋轉(zhuǎn))實例分析
- Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過程
- Android自定義View實現(xiàn)葉子飄動旋轉(zhuǎn)效果(四)
- Android自定義View實現(xiàn)多片葉子旋轉(zhuǎn)滑動(五)
- Android中利用matrix 控制圖片的旋轉(zhuǎn)、縮放、移動
- Android 圖片縮放與旋轉(zhuǎn)的實現(xiàn)詳解
- Android UI之ImageView實現(xiàn)圖片旋轉(zhuǎn)和縮放
- Android實現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- 基于Android 實現(xiàn)圖片平移、縮放、旋轉(zhuǎn)同時進(jìn)行
- Android實現(xiàn)旋轉(zhuǎn),放大,縮小圖片的方法
相關(guān)文章
Android MVP BaseFragment 通用式封裝的實現(xiàn)
這篇文章主要介紹了Android MVP BaseFragment 通用式封裝的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android開發(fā)中實現(xiàn)發(fā)送短信的小程序示例
這篇文章主要介紹了Android開發(fā)中實現(xiàn)發(fā)送短信的小程序示例,文中還附帶了一個監(jiān)聽廣播接收者的升級版短信發(fā)送例子,需要的朋友可以參考下2016-04-04
SurfaceView開發(fā)[捉小豬]手機游戲 (一)
這篇文章主要介紹了用SurfaceView開發(fā)[捉小豬]手機游戲 (一)本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08

