引用:http://www.open-open.com/lib/view/open1325749794953.html
android 实现图片的翻转
1 |
Resources res = this .getContext().getResources(); |
2 |
img = BitmapFactory.decodeResource(res, R.drawable.aa); |
3 |
Matrix matrix = new Matrix(); |
4 |
matrix.postRotate( 180 ); |
5 |
int width = img.getWidth(); |
6 |
int height = img.getHeight(); |
7 |
img_a = Bitmap.createBitmap(img, 0 , 0 , width, height, matrix, true ); |
然后可以直接把img_a draw到画布上,canvas.drawBitmap(img_a, 10, 10, p);
Matrix 是一个处理翻转、缩放等图像效果的重要类
Matrix.postScale 可设置缩放比例,默认为1
**********************************************************************
android 实现图片的旋转
01 |
public class ex04_22 extends Activity{ |
03 |
private ImageView mImageView; |
04 |
private Button btn1,btn2; |
05 |
private TextView mTextView; |
06 |
private AbsoluteLayout layout1; |
07 |
private int ScaleTimes= 1 ,ScaleAngle= 1 ; |
09 |
public void onCreate(Bundle savedInstanceState) { |
10 |
super .onCreate(savedInstanceState); |
11 |
setContentView(R.layout.main); |
12 |
mImageView=(ImageView)findViewById(R.id.myImageView); |
13 |
final Bitmap bmp=BitmapFactory.decodeResource( this .getResources(),R.drawable.ex04_22_1); |
14 |
final int widthOrig=bmp.getWidth(); |
15 |
final int heightOrig=bmp.getHeight(); |
16 |
mImageView.setImageBitmap(bmp); |
17 |
btn1=(Button)findViewById(R.id.myButton1); |
18 |
btn1.setOnClickListener( new OnClickListener(){ |
19 |
public void onClick(View v){ |
24 |
int newWidth=widthOrig*ScaleTimes; |
25 |
int newHeight=heightOrig*ScaleTimes; |
26 |
float scaleWidth=(( float )newWidth)/widthOrig; |
27 |
float scaleHeight=(( float )newHeight)/heightOrig; |
28 |
Matrix matrix= new Matrix(); |
29 |
matrix.postScale(scaleWidth, scaleHeight); |
30 |
matrix.setRotate( 5 *ScaleAngle); |
31 |
Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0 , 0 , widthOrig, heightOrig, matrix, true ); |
32 |
BitmapDrawable myNewBitmapDrawable= new BitmapDrawable(resizeBitmap); |
33 |
mImageView.setImageDrawable(myNewBitmapDrawable); |
36 |
btn2=(Button)findViewById(R.id.myButton2); |
37 |
btn2.setOnClickListener( new OnClickListener(){ |
38 |
public void onClick(View v){ |
43 |
int newWidth=widthOrig*ScaleTimes; |
44 |
int newHeight=heightOrig*ScaleTimes; |
45 |
float scaleWidth=(( float )newWidth)/widthOrig; |
46 |
float scaleHeight=(( float )newHeight)/heightOrig; |
47 |
Matrix matrix= new Matrix(); |
48 |
matrix.postScale(scaleWidth, scaleHeight); |
49 |
matrix.setRotate( 5 *ScaleAngle); |
50 |
Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0 , 0 , widthOrig, heightOrig, matrix, true ); |
51 |
BitmapDrawable myNewBitmapDrawable= new BitmapDrawable(resizeBitmap); |
52 |
mImageView.setImageDrawable(myNewBitmapDrawable); |
**********************************************************************
实现画面淡入淡出效果可以用 :setAlpha(alpha);
alpha从255,逐渐递减!
**********************************************************************
如何实现屏幕的滚动效果,这里有两个关键点,一个是实现OnGestureListener,
以便在触摸事件发生的时候,被回调。包括按下,滚动等等,按照API文档,
需要分两步来实现检测手势行为。
1)创建GestureDetector实例
2) 在onTouchEvent()方法中调用GestureDetector的onTouchEvent()方法。
另一个关键点是自己实现一个简单的View,来绘制图片。
代码如下所示。由于,我们不需要使用layout定义,所以不需要提供xml文件。
直接在程序里面setContentView()即可。
003 |
import android.app.Activity; |
004 |
import android.content.Context; |
005 |
import android.content.res.Resources; |
006 |
import android.graphics.Bitmap; |
007 |
import android.graphics.BitmapFactory; |
008 |
import android.graphics.Canvas; |
009 |
import android.graphics.Paint; |
010 |
import android.os.Bundle; |
011 |
import android.view.GestureDetector; |
012 |
import android.view.MotionEvent; |
013 |
import android.view.View; |
014 |
import android.view.ViewGroup; |
015 |
import android.view.GestureDetector.OnGestureListener; |
017 |
public class HorizontalScroll extends Activity implements OnGestureListener { |
018 |
private static final int X_MAX = 800 ; |
019 |
private static final int Y_MAX = 600 ; |
020 |
private int scrollX = 0 ; |
021 |
private int scrollY = 0 ; |
028 |
GestureDetector gestureScanner; |
031 |
public void onCreate(Bundle savedInstanceState) { |
032 |
super .onCreate(savedInstanceState); |
034 |
gestureScanner = new GestureDetector( this ); |
037 |
res = getResources(); |
038 |
bmp = BitmapFactory.decodeResource(res, R.drawable.arc); |
039 |
adapt = Bitmap.createBitmap(bmp); |
041 |
main = new MyView( this ); |
042 |
setContentView(main, new ViewGroup.LayoutParams( 800 , 600 )); |
046 |
public boolean onTouchEvent(MotionEvent me) { |
047 |
return gestureScanner.onTouchEvent(me); |
050 |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, |
052 |
main.handleScroll(distanceX, distanceY); |
056 |
public boolean onDown(MotionEvent e) { |
060 |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, |
065 |
public void onLongPress(MotionEvent e) { |
068 |
public void onShowPress(MotionEvent e) { |
071 |
public boolean onSingleTapUp(MotionEvent e) { |
079 |
class MyView extends View { |
080 |
public MyView(Context context) { |
085 |
protected void onDraw(Canvas canvas) { |
086 |
canvas.drawBitmap(adapt, -scrollX, -scrollY, paint); |
089 |
public void handleScroll( float distX, float distY) { |
096 |
} else if (distX < - 6.0 ) { |
108 |
} else if (distY < - 6.0 ) { |
**********************************************************************
教你在谷歌Android平台中处理图片
操作图像像素
现在你可以对单独的像素进行处理了。通过使用android.graphics.Bitmap API中的
getPixels,可以加载像素到一个整数数组中。在本文例子中,你将按照一定规则对每一
个像素实现着色。经过这个处理后,所有的像素将被转化为一个范围在0到255的字节码。
android.graphics.Bitmap API中的setPixels则用来加载这个整数数组到一个图像中。
最后一步是通过ImageView变量mIV来更新屏幕。以下是实现这个染色过程的代码片段。
01 |
private void TintThePicture( int deg) { |
02 |
int [] pix = new int [picw * pich]; |
03 |
mBitmap.getPixels(pix, 0 , picw, 0 , 0 , picw, pich); |
05 |
int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y; |
06 |
double angle = ( 3 .14159d * ( double )deg) / 180 .0d; |
07 |
int S = ( int )( 256 .0d * Math.sin(angle)); |
08 |
int C = ( int )( 256 .0d * Math.cos(angle)); |
10 |
for ( int y = 0 ; y < pich; y++) |
11 |
for ( int x = 0 ; x < picw; x++) |
13 |
int index = y * picw + x; |
14 |
int r = (pix[index] >> 16 ) & 0xff ; |
15 |
int g = (pix[index] >> 8 ) & 0xff ; |
16 |
int b = pix[index] & 0xff ; |
17 |
RY = ( 70 * r - 59 * g - 11 * b) / 100 ; |
18 |
GY = (- 30 * r + 41 * g - 11 * b) / 100 ; |
19 |
BY = (- 30 * r - 59 * g + 89 * b) / 100 ; |
20 |
Y = ( 30 * r + 59 * g + 11 * b) / 100 ; |
21 |
RYY = (S * BY + C * RY) / 256 ; |
22 |
BYY = (C * BY - S * RY) / 256 ; |
23 |
GYY = (- 51 * RYY - 19 * BYY) / 100 ; |
25 |
R = (R < 0 ) ? 0 : ((R > 255 ) ? 255 : R); |
27 |
G = (G < 0 ) ? 0 : ((G > 255 ) ? 255 : G); |
29 |
B = (B < 0 ) ? 0 : ((B > 255 ) ? 255 : B); |
30 |
pix[index] = 0xff000000 | (R << 16 ) | (G << 8 ) | B; |
33 |
Bitmap bm = Bitmap.createBitmap(picw, pich, false ); |
34 |
bm.setPixels(pix, 0 , picw, 0 , 0 , picw, pich); |
37 |
mIV.setImageBitmap(bm); |
**********************************************************************
android 图片的放大和缩小
01 |
public class ex04_22 extends Activity{ |
02 |
private ImageView mImageView; |
03 |
private Button btn1,btn2; |
04 |
private TextView mTextView; |
05 |
private AbsoluteLayout layout1; |
08 |
private int displayWidth,displayHeight; |
09 |
private float scaleWidth= 1 ,scaleHeight= 1 ; |
10 |
private final static String filename= "/data/data/ex04_22.lcs/ex04_22_2.png" ; |
12 |
public void onCreate(Bundle savedInstanceState) { |
13 |
super .onCreate(savedInstanceState); |
14 |
setContentView(R.layout.main); |
16 |
DisplayMetrics dm= new DisplayMetrics(); |
17 |
getWindowManager().getDefaultDisplay().getMetrics(dm); |
18 |
displayWidth=dm.widthPixels; |
19 |
displayHeight=dm.heightPixels- 80 ; |
20 |
bmp=BitmapFactory.decodeResource( this .getResources(),R.drawable.ex04_22_1); |
21 |
layout1=(AbsoluteLayout)findViewById(R.id.layout1); |
22 |
mImageView=(ImageView)findViewById(R.id.myImageView); |
23 |
btn1=(Button)findViewById(R.id.myButton1); |
24 |
btn1.setOnClickListener( new OnClickListener(){ |
25 |
public void onClick(View v){ |
29 |
btn2=(Button)findViewById(R.id.myButton2); |
30 |
btn2.setOnClickListener( new OnClickListener(){ |
31 |
public void onClick(View v){ |
38 |
int bmpWidth=bmp.getWidth(); |
39 |
int bmpHeight=bmp.getHeight(); |
43 |
scaleWidth=( float )(scaleWidth*scale); |
44 |
scaleHeight=( float )(scaleHeight*scale); |
46 |
Matrix matrix= new Matrix(); |
47 |
matrix.postScale(scaleWidth, scaleHeight); |
48 |
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0 , 0 , bmpWidth, bmpHeight, matrix, true ); |
50 |
layout1.removeView(mImageView); |
53 |
layout1.removeView((ImageView)findViewById(id)); |
56 |
ImageView imageView= new ImageView( this ); |
58 |
imageView.setImageBitmap(resizeBmp); |
59 |
layout1.addView(imageView); |
60 |
setContentView(layout1); |
61 |
btn2.setEnabled( true ); |
65 |
int bmpWidth=bmp.getWidth(); |
66 |
int bmpHeight=bmp.getHeight(); |
70 |
scaleWidth=( float )(scaleWidth*scale); |
71 |
scaleHeight=( float )(scaleHeight*scale); |
73 |
Matrix matrix= new Matrix(); |
74 |
matrix.postScale(scaleWidth, scaleHeight); |
75 |
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0 , 0 , bmpWidth, bmpHeight, matrix, true ); |
77 |
layout1.removeView(mImageView); |
80 |
layout1.removeView((ImageView)findViewById(id)); |
83 |
ImageView imageView= new ImageView( this ); |
85 |
imageView.setImageBitmap(resizeBmp); |
86 |
layout1.addView(imageView); |
87 |
setContentView(layout1); |
88 |
if (scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*scaleHeight>displayHeight){ |
89 |
btn2.setEnabled( false ); |
xml文件
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
04 |
android:id = "@+id/layout1" |
05 |
android:layout_width = "fill_parent" |
06 |
android:layout_height = "fill_parent" |
07 |
xmlns:android = "http://schemas.android.com/apk/res/android" |
10 |
android:id = "@+id/myImageView" |
11 |
android:layout_width = "200px" |
13 |
android:layout_height = "150px" |
14 |
android:src = "@drawable/ex04_22_1" |
15 |
android:layout_x = "0px" |
16 |
android:layout_y = "0px" |
20 |
android:id = "@+id/myButton1" |
22 |
android:layout_width = "90px" |
23 |
android:layout_height = "60px" |
25 |
android:textSize = "18sp" |
26 |
android:layout_x = "20px" |
27 |
android:layout_y = "372px" |
32 |
android:id = "@+id/myButton2" |
33 |
android:layout_width = "90px" |
34 |
android:layout_height = "60px" |
36 |
android:textSize = "18sp" |
38 |
android:layout_x = "210px" |
39 |
android:layout_y = "372px" |
*********************************************************************
android 图片透明度处理代码
01 |
public static Bitmap setAlpha(Bitmap sourceImg, int number) { |
03 |
int [] argb = new int [sourceImg.getWidth() * sourceImg.getHeight()]; |
05 |
sourceImg.getPixels(argb, 0 , sourceImg.getWidth(), 0 , 0 ,sourceImg.getWidth(), sourceImg.getHeight()); |
07 |
number = number * 255 / 100 ; |
09 |
for ( int i = 0 ; i < argb.length; i++) { |
11 |
argb = (number << 24 ) | (argb & 0x00FFFFFF ); |
15 |
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888); |