欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现图片反转、翻转、旋转、放大和缩小

程序员文章站 2023-11-21 23:21:52
********************************************************************** android 实现图片...

**********************************************************************

android 实现图片的翻转

**********************************************************************

resources res = this.getcontext().getresources();
 img = bitmapfactory.decoderesource(res, r.drawable.aa);
 matrix matrix = new matrix();
 matrix.postrotate(180); /*翻转180度*/
 int width = img.getwidth();
 int height = img.getheight();
 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 实现图片的旋转

**********************************************************************

public class ex04_22 extends activity{
 
private imageview mimageview;
private button btn1,btn2;
private textview mtextview;
private absolutelayout layout1;
private int scaletimes=1,scaleangle=1;
 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 mimageview=(imageview)findviewbyid(r.id.myimageview);
 final bitmap bmp=bitmapfactory.decoderesource(this.getresources(),r.drawable.ex04_22_1);
 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);
 }
 });
 }

**********************************************************************

实现画面淡入淡出效果可以用 :setalpha(alpha);
alpha从255,逐渐递减!

**********************************************************************

如何实现屏幕的滚动效果,这里有两个关键点,一个是实现ongesturelistener,以便在触摸事件发生的时候,被回调。包括按下,滚动等等,按照api文档,需要分两步来实现检测手势行为。

1)创建gesturedetector实例

2) 在ontouchevent()方法中调用gesturedetector的ontouchevent()方法。

另一个关键点是自己实现一个简单的view,来绘制图片。

代码如下所示。由于,我们不需要使用layout定义,所以不需要提供xml文件。
直接在程序里面setcontentview()即可。

package com.j2medev;
 
import android.app.activity;
import android.content.context;
import android.content.res.resources;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.paint;
import android.os.bundle;
import android.view.gesturedetector;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.view.gesturedetector.ongesturelistener;
 
public class horizontalscroll extends activity implements ongesturelistener {
private static final int x_max = 800;
private static final int y_max = 600;
private int scrollx = 0;
private int scrolly = 0;
 
myview main;
bitmap bmp;
bitmap adapt;
resources res;
paint paint;
gesturedetector gesturescanner;
 
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
 
gesturescanner = new gesturedetector(this);
paint = new paint();
 
res = getresources();
bmp = bitmapfactory.decoderesource(res, r.drawable.arc);
adapt = bitmap.createbitmap(bmp);
 
main = new myview(this);
setcontentview(main, new viewgroup.layoutparams(800, 600));
}
 
@override
public boolean ontouchevent(motionevent me) {
return gesturescanner.ontouchevent(me);
}
 
public boolean onscroll(motionevent e1, motionevent e2, float distancex,
 float distancey) {
main.handlescroll(distancex, distancey);
return true;
}
 
public boolean ondown(motionevent e) {
return true;
}
 
public boolean onfling(motionevent e1, motionevent e2, float velocityx,
 float velocityy) {
return true;
}
 
public void onlongpress(motionevent e) {
}
 
public void onshowpress(motionevent e) {
}
 
public boolean onsingletapup(motionevent e) {
return true;
}
 
// //////////////////
// /////////////////
// ////////////////
 
class myview extends view {
public myview(context context) {
 super(context);
}
 
@override
protected void ondraw(canvas canvas) {
 canvas.drawbitmap(adapt, -scrollx, -scrolly, paint);
}
 
public void handlescroll(float distx, float disty) {
 // x-axis ////////////////////////////////
 
 if (distx > 6.0) {
 if (scrollx < 460) {
 scrollx += 15;
 }
 } else if (distx < -6.0) {
 if (scrollx >= 15) {
 scrollx -= 15;
 }
 }
 // //////////////////////////////////////////
 
 // y-axis //////////////////////////////////
 if (disty > 6.0) {
 if (scrolly < 100) {
 scrolly += 15;
 }
 } else if (disty < -6.0) {
 if (scrolly >= 15) {
 scrolly -= 15;
 }
 }
 // //////////////////////////////////////////
 //
 // if ((scrollx <= 480) && (scrolly <= 120)) {
 // adapt = bitmap.createbitmap(bmp, scrollx, scrolly, 320, 480);
 // invalidate();
 // }
 invalidate();
}
}
}

**********************************************************************

教你在谷歌android平台中处理图片

**********************************************************************

操作图像像素

现在你可以对单独的像素进行处理了。通过使用android.graphics.bitmap api中的getpixels,可以加载像素到一个整数数组中。

在本文例子中,你将按照一定规则对每一个像素实现着色。经过这个处理后,所有的像素将被转化为一个范围在0到255的字节码。

android.graphics.bitmap api中的setpixels则用来加载这个整数数组到一个图像中。

最后一步是通过imageview变量miv来更新屏幕。以下是实现这个染色过程的代码片段。

private void tintthepicture(int deg) {
 int[] pix = new int[picw * pich];
 mbitmap.getpixels(pix, 0, picw, 0, 0, picw, pich);
 
 int ry, gy, by, ryy, gyy, byy, r, g, b, y;
 double angle = (3.14159d * (double)deg) / 180.0d;
 int s = (int)(256.0d * math.sin(angle));
 int c = (int)(256.0d * math.cos(angle));
 
 for (int y = 0; y < pich; y++)
 for (int x = 0; x < picw; x++)
 {
 int index = y * picw + x;
 int r = (pix[index] >> 16) & 0xff;
 int g = (pix[index] >> 8) & 0xff;
 int b = pix[index] & 0xff;
 ry = ( 70 * r - 59 * g - 11 * b) / 100;
 gy = (-30 * r + 41 * g - 11 * b) / 100;
 by = (-30 * r - 59 * g + 89 * b) / 100;
 y = ( 30 * r + 59 * g + 11 * b) / 100;
 ryy = (s * by + c * ry) / 256;
 byy = (c * by - s * ry) / 256;
 gyy = (-51 * ryy - 19 * byy) / 100;
 r = y + ryy;
 r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
 g = y + gyy;
 g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
 b = y + byy;
 b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
 pix[index] = 0xff000000 | (r << 16) | (g << 8) | b;
 }
 
 bitmap bm = bitmap.createbitmap(picw, pich, false);
 bm.setpixels(pix, 0, picw, 0, 0, picw, pich);
 
 // put the updated bitmap into the main view
 miv.setimagebitmap(bm);
 miv.invalidate();
 
 mbitmap = bm;
 pix = null;
 }

**********************************************************************

android 图片的放大和缩小

**********************************************************************

public class ex04_22 extends activity{
private imageview mimageview;
private button btn1,btn2;
private textview mtextview;
private absolutelayout layout1;
private bitmap bmp;
private int id=0;
private int displaywidth,displayheight;
private float scalewidth=1,scaleheight=1;
private final static string filename="/data/data/ex04_22.lcs/ex04_22_2.png";
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
//取得屏幕分辨率
displaymetrics dm=new displaymetrics();
getwindowmanager().getdefaultdisplay().getmetrics(dm);
displaywidth=dm.widthpixels;
displayheight=dm.heightpixels-80;
bmp=bitmapfactory.decoderesource(this.getresources(),r.drawable.ex04_22_1);
layout1=(absolutelayout)findviewbyid(r.id.layout1);
mimageview=(imageview)findviewbyid(r.id.myimageview);
btn1=(button)findviewbyid(r.id.mybutton1);
btn1.setonclicklistener(new onclicklistener(){
public void onclick(view v){
small();
}
});
btn2=(button)findviewbyid(r.id.mybutton2);
btn2.setonclicklistener(new onclicklistener(){
public void onclick(view v){
big();
}
});
}
private void small(){
//获得bitmap的高和宽
int bmpwidth=bmp.getwidth();
int bmpheight=bmp.getheight();
//设置缩小比例
double scale=0.8;
//计算出这次要缩小的比例
scalewidth=(float)(scalewidth*scale);
scaleheight=(float)(scaleheight*scale);
//产生resize后的bitmap对象
matrix matrix=new matrix();
matrix.postscale(scalewidth, scaleheight);
bitmap resizebmp=bitmap.createbitmap(bmp, 0, 0, bmpwidth, bmpheight, matrix, true);
if(id==0){
layout1.removeview(mimageview);
}
else{
layout1.removeview((imageview)findviewbyid(id));
}
id++;
imageview imageview=new imageview(this);
imageview.setid(id);
imageview.setimagebitmap(resizebmp);
layout1.addview(imageview);
setcontentview(layout1);
btn2.setenabled(true);
}
private void big(){
//获得bitmap的高和宽
int bmpwidth=bmp.getwidth();
int bmpheight=bmp.getheight();
//设置缩小比例
double scale=1.25;
//计算出这次要缩小的比例
scalewidth=(float)(scalewidth*scale);
scaleheight=(float)(scaleheight*scale);
//产生resize后的bitmap对象
matrix matrix=new matrix();
matrix.postscale(scalewidth, scaleheight);
bitmap resizebmp=bitmap.createbitmap(bmp, 0, 0, bmpwidth, bmpheight, matrix, true);
if(id==0){
layout1.removeview(mimageview);
}
else{
layout1.removeview((imageview)findviewbyid(id));
}
id++;
imageview imageview=new imageview(this);
imageview.setid(id);
imageview.setimagebitmap(resizebmp);
layout1.addview(imageview);
setcontentview(layout1);
if(scalewidth*scale*bmpwidth>displaywidth||scaleheight*scale*scaleheight>displayheight){
btn2.setenabled(false);
}
}
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
 
<absolutelayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<imageview
android:id="@+id/myimageview"
android:layout_width="200px"
 
android:layout_height="150px"
android:src="@drawable/ex04_22_1"
android:layout_x="0px"
android:layout_y="0px"
>
</imageview>
<button
android:id="@+id/mybutton1"
 
android:layout_width="90px"
android:layout_height="60px"
android:text="缩小"
android:textsize="18sp"
android:layout_x="20px"
android:layout_y="372px"
 
>
</button>
<button
android:id="@+id/mybutton2"
android:layout_width="90px"
android:layout_height="60px"
android:text="放大"
android:textsize="18sp"
 
android:layout_x="210px"
android:layout_y="372px"
>
</button>
</absolutelayout>

*********************************************************************

android 图片透明度处理代码

*********************************************************************

public static bitmap setalpha(bitmap sourceimg, int number) {
 
  int[] argb = new int[sourceimg.getwidth() * sourceimg.getheight()];
 
  sourceimg.getpixels(argb, 0, sourceimg.getwidth(), 0, 0,sourceimg.getwidth(), sourceimg.getheight());// 获得图片的argb值
 
  number = number * 255 / 100;
 
  for (int i = 0; i < argb.length; i++) {
 
  argb = (number << 24) | (argb & 0x00ffffff);// 修改最高2位的值
 
  }
 
  sourceimg = bitmap.createbitmap(argb, sourceimg.getwidth(), sourceimg.getheight(), config.argb_8888);
 
  return sourceimg;
 
}

以上就是涉及到了android图片处理的所有内容,包括android图片反转、android 图片翻转、android 图片旋转、实现画面淡入淡出效果、android 图片的放大和缩小以及教你在谷歌android平台中处理图片。