引用: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 ); /*翻转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{ |
02 |
|
03 |
private ImageView mImageView; |
04 |
private Button btn1,btn2; |
05 |
private TextView mTextView; |
06 |
private AbsoluteLayout layout1; |
07 |
private int ScaleTimes= 1 ,ScaleAngle= 1 ; |
08 |
@Override |
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){ |
20 |
ScaleAngle--; |
21 |
if (ScaleAngle<- 60 ){ |
22 |
ScaleAngle=- 60 ; |
23 |
} |
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); |
34 |
} |
35 |
}); |
36 |
btn2=(Button)findViewById(R.id.myButton2); |
37 |
btn2.setOnClickListener( new OnClickListener(){ |
38 |
public void onClick(View v){ |
39 |
ScaleAngle++; |
40 |
if (ScaleAngle> 60 ){ |
41 |
ScaleAngle= 60 ; |
42 |
} |
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); |
53 |
} |
54 |
}); |
55 |
} |
**********************************************************************
实现画面淡入淡出效果可以用 :setAlpha(alpha);
alpha从255,逐渐递减!
**********************************************************************
如何实现屏幕的滚动效果,这里有两个关键点,一个是实现OnGestureListener,
以便在触摸事件发生的时候,被回调。包括按下,滚动等等,按照API文档,
需要分两步来实现检测手势行为。
1)创建GestureDetector实例
2) 在onTouchEvent()方法中调用GestureDetector的onTouchEvent()方法。
另一个关键点是自己实现一个简单的View,来绘制图片。
代码如下所示。由于,我们不需要使用layout定义,所以不需要提供xml文件。
直接在程序里面setContentView()即可。
001 |
package com.j2medev; |
002 |
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; |
016 |
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 ; |
022 |
023 |
MyView main; |
024 |
Bitmap bmp; |
025 |
Bitmap adapt; |
026 |
Resources res; |
027 |
Paint paint; |
028 |
GestureDetector gestureScanner; |
029 |
030 |
@Override |
031 |
public void onCreate(Bundle savedInstanceState) { |
032 |
super .onCreate(savedInstanceState); |
033 |
034 |
gestureScanner = new GestureDetector( this ); |
035 |
paint = new Paint(); |
036 |
037 |
res = getResources(); |
038 |
bmp = BitmapFactory.decodeResource(res, R.drawable.arc); |
039 |
adapt = Bitmap.createBitmap(bmp); |
040 |
041 |
main = new MyView( this ); |
042 |
setContentView(main, new ViewGroup.LayoutParams( 800 , 600 )); |
043 |
} |
044 |
045 |
@Override |
046 |
public boolean onTouchEvent(MotionEvent me) { |
047 |
return gestureScanner.onTouchEvent(me); |
048 |
} |
049 |
050 |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, |
051 |
float distanceY) { |
052 |
main.handleScroll(distanceX, distanceY); |
053 |
return true ; |
054 |
} |
055 |
056 |
public boolean onDown(MotionEvent e) { |
057 |
return true ; |
058 |
} |
059 |
060 |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, |
061 |
float velocityY) { |
062 |
return true ; |
063 |
} |
064 |
065 |
public void onLongPress(MotionEvent e) { |
066 |
} |
067 |
068 |
public void onShowPress(MotionEvent e) { |
069 |
} |
070 |
071 |
public boolean onSingleTapUp(MotionEvent e) { |
072 |
return true ; |
073 |
} |
074 |
075 |
// ////////////////// |
076 |
// ///////////////// |
077 |
// //////////////// |
078 |
079 |
class MyView extends View { |
080 |
public MyView(Context context) { |
081 |
super (context); |
082 |
} |
083 |
084 |
@Override |
085 |
protected void onDraw(Canvas canvas) { |
086 |
canvas.drawBitmap(adapt, -scrollX, -scrollY, paint); |
087 |
} |
088 |
089 |
public void handleScroll( float distX, float distY) { |
090 |
// X-Axis //////////////////////////////// |
091 |
092 |
if (distX > 6.0 ) { |
093 |
if (scrollX < 460 ) { |
094 |
scrollX += 15 ; |
095 |
} |
096 |
} else if (distX < - 6.0 ) { |
097 |
if (scrollX >= 15 ) { |
098 |
scrollX -= 15 ; |
099 |
} |
100 |
} |
101 |
// ////////////////////////////////////////// |
102 |
103 |
// Y-AXIS ////////////////////////////////// |
104 |
if (distY > 6.0 ) { |
105 |
if (scrollY < 100 ) { |
106 |
scrollY += 15 ; |
107 |
} |
108 |
} else if (distY < - 6.0 ) { |
109 |
if (scrollY >= 15 ) { |
110 |
scrollY -= 15 ; |
111 |
} |
112 |
} |
113 |
// ////////////////////////////////////////// |
114 |
// |
115 |
// if ((scrollX <= 480) && (scrollY <= 120)) { |
116 |
// adapt = Bitmap.createBitmap(bmp, scrollX, scrollY, 320, 480); |
117 |
// invalidate(); |
118 |
// } |
119 |
invalidate(); |
120 |
} |
121 |
} |
122 |
} |
**********************************************************************
教你在谷歌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); |
04 |
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)); |
09 |
10 |
for ( int y = 0 ; y < pich; y++) |
11 |
for ( int x = 0 ; x < picw; x++) |
12 |
{ |
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 ; |
24 |
R = Y + RYY; |
25 |
R = (R < 0 ) ? 0 : ((R > 255 ) ? 255 : R); |
26 |
G = Y + GYY; |
27 |
G = (G < 0 ) ? 0 : ((G > 255 ) ? 255 : G); |
28 |
B = Y + BYY; |
29 |
B = (B < 0 ) ? 0 : ((B > 255 ) ? 255 : B); |
30 |
pix[index] = 0xff000000 | (R << 16 ) | (G << 8 ) | B; |
31 |
} |
32 |
|
33 |
Bitmap bm = Bitmap.createBitmap(picw, pich, false ); |
34 |
bm.setPixels(pix, 0 , picw, 0 , 0 , picw, pich); |
35 |
|
36 |
// Put the updated bitmap into the main view |
37 |
mIV.setImageBitmap(bm); |
38 |
mIV.invalidate(); |
39 |
|
40 |
mBitmap = bm; |
41 |
pix = null ; |
42 |
} |
**********************************************************************
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; |
06 |
private Bitmap bmp; |
07 |
private int id= 0 ; |
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" ; |
11 |
@Override |
12 |
public void onCreate(Bundle savedInstanceState) { |
13 |
super .onCreate(savedInstanceState); |
14 |
setContentView(R.layout.main); |
15 |
//取得屏幕分辨率 |
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){ |
26 |
small(); |
27 |
} |
28 |
}); |
29 |
btn2=(Button)findViewById(R.id.myButton2); |
30 |
btn2.setOnClickListener( new OnClickListener(){ |
31 |
public void onClick(View v){ |
32 |
big(); |
33 |
} |
34 |
}); |
35 |
} |
36 |
private void small(){ |
37 |
//获得Bitmap的高和宽 |
38 |
int bmpWidth=bmp.getWidth(); |
39 |
int bmpHeight=bmp.getHeight(); |
40 |
//设置缩小比例 |
41 |
double scale= 0.8 ; |
42 |
//计算出这次要缩小的比例 |
43 |
scaleWidth=( float )(scaleWidth*scale); |
44 |
scaleHeight=( float )(scaleHeight*scale); |
45 |
//产生resize后的Bitmap对象 |
46 |
Matrix matrix= new Matrix(); |
47 |
matrix.postScale(scaleWidth, scaleHeight); |
48 |
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0 , 0 , bmpWidth, bmpHeight, matrix, true ); |
49 |
if (id== 0 ){ |
50 |
layout1.removeView(mImageView); |
51 |
} |
52 |
else { |
53 |
layout1.removeView((ImageView)findViewById(id)); |
54 |
} |
55 |
id++; |
56 |
ImageView imageView= new ImageView( this ); |
57 |
imageView.setId(id); |
58 |
imageView.setImageBitmap(resizeBmp); |
59 |
layout1.addView(imageView); |
60 |
setContentView(layout1); |
61 |
btn2.setEnabled( true ); |
62 |
} |
63 |
private void big(){ |
64 |
//获得Bitmap的高和宽 |
65 |
int bmpWidth=bmp.getWidth(); |
66 |
int bmpHeight=bmp.getHeight(); |
67 |
//设置缩小比例 |
68 |
double scale= 1.25 ; |
69 |
//计算出这次要缩小的比例 |
70 |
scaleWidth=( float )(scaleWidth*scale); |
71 |
scaleHeight=( float )(scaleHeight*scale); |
72 |
//产生resize后的Bitmap对象 |
73 |
Matrix matrix= new Matrix(); |
74 |
matrix.postScale(scaleWidth, scaleHeight); |
75 |
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0 , 0 , bmpWidth, bmpHeight, matrix, true ); |
76 |
if (id== 0 ){ |
77 |
layout1.removeView(mImageView); |
78 |
} |
79 |
else { |
80 |
layout1.removeView((ImageView)findViewById(id)); |
81 |
} |
82 |
id++; |
83 |
ImageView imageView= new ImageView( this ); |
84 |
imageView.setId(id); |
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 ); |
90 |
} |
91 |
} |
92 |
} |
xml文件
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
03 |
< AbsoluteLayout |
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" |
08 |
> |
09 |
< ImageView |
10 |
android:id = "@+id/myImageView" |
11 |
android:layout_width = "200px" |
12 |
13 |
android:layout_height = "150px" |
14 |
android:src = "@drawable/ex04_22_1" |
15 |
android:layout_x = "0px" |
16 |
android:layout_y = "0px" |
17 |
> |
18 |
</ ImageView > |
19 |
< Button |
20 |
android:id = "@+id/myButton1" |
21 |
22 |
android:layout_width = "90px" |
23 |
android:layout_height = "60px" |
24 |
android:text = "缩小" |
25 |
android:textSize = "18sp" |
26 |
android:layout_x = "20px" |
27 |
android:layout_y = "372px" |
28 |
29 |
> |
30 |
</ Button > |
31 |
< Button |
32 |
android:id = "@+id/myButton2" |
33 |
android:layout_width = "90px" |
34 |
android:layout_height = "60px" |
35 |
android:text = "放大" |
36 |
android:textSize = "18sp" |
37 |
38 |
android:layout_x = "210px" |
39 |
android:layout_y = "372px" |
40 |
> |
41 |
</ Button > |
42 |
</ AbsoluteLayout > |
*********************************************************************
android 图片透明度处理代码
01 |
public static Bitmap setAlpha(Bitmap sourceImg, int number) { |
02 |
03 |
int [] argb = new int [sourceImg.getWidth() * sourceImg.getHeight()]; |
04 |
05 |
sourceImg.getPixels(argb, 0 , sourceImg.getWidth(), 0 , 0 ,sourceImg.getWidth(), sourceImg.getHeight()); // 获得图片的ARGB值 |
06 |
07 |
number = number * 255 / 100 ; |
08 |
09 |
for ( int i = 0 ; i < argb.length; i++) { |
10 |
11 |
argb = (number << 24 ) | (argb & 0x00FFFFFF ); // 修改最高2位的值 |
12 |
13 |
} |
14 |
15 |
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888); |
16 |
17 |
return sourceImg; |
18 |
19 |
} |