您的位置:首页 > 移动开发 > Android开发

android图片转换代码

2012-02-29 17:06 67 查看
android图片转换代码

package com.sl.test.ui.manage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import com.baidu.mapapi.GeoPoint;
import com.sl.quwei.R;
import df.util.type.StringUtil;
import org.xbill.DNS.utils.base64;

import java.io.ByteArrayOutputStream;

/**
* Created by IntelliJ IDEA.
* User: wulong
* Date: 11-10-27
* Time: 上午6:31
* To change this template use File | Settings | File Templates.
*/
public class AggTools {
/**
* @param val
* @return
*/
public final static double latLangFromIntToDouble(int val) {
return (double) (val / 1E6);
}

public final static int latLangFromDoubleToInt(double val) {
return (int) (val * 1E6);
}

public static boolean setTabBackgroundColor(Context context, TabHost tabhost, int focusColor, int releaseColor) {
final TabWidget tabwidget = tabhost.getTabWidget();

for (int i = 0; i < tabwidget.getChildCount(); i++) {
View v = tabwidget.getChildAt(i);
if (tabhost.getCurrentTab() == i) {
v.setBackgroundColor(context.getResources().getColor(R.color.color_tab_light_orange));//R.color.color_tab_light_orange);
//更改tab标题颜色
final TextView tv = (TextView) tabwidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(context.getResources().getColorStateList(android.R.color.darker_gray));
} else {
v.setBackgroundColor(context.getResources().getColor(R.color.color_tab_brown));//0xffE59500);//R.color.color_tab_brown);
//更改tab标题颜色
final TextView tv = (TextView) tabwidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(context.getResources().getColorStateList(android.R.color.white));
}
}

return true;
}

public static String getNewTitle(CharSequence oldTitle, String loginName) {
String title = "" + oldTitle;
String showTitle;

if (title.contains(loginName)) {
showTitle = title;
} else {
showTitle = title + ":" + loginName;
}
return showTitle;
}

public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
//        Canvas canvas = new Canvas(bitmap);
//        //canvas.setBitmap(bitmap);
//        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//        drawable.draw(canvas);
return bitmap;
}

/**
*  字符串转bitmap
* @param in
* @return
*/
public static Bitmap convertAvatarFromStringToBitmap(String in) {
if (StringUtil.empty(in)) {
return null;
}

byte[] byteBuf = base64.fromString(in);
if (null == byteBuf) {
return null;
}

Bitmap bitImg = bytes2Bimap(byteBuf);
return bitImg;
}

/**
* 将图片按指定质量转成字符串格式
* @param src
* @param quality
* @return
*/
public static String convertAvatarFromBitmapToString(Bitmap src, int quality) {
if (null == src) {
return "";
}

if (quality < 0) {
quality = 0;
} else if (quality > 100) {
quality = 100;
}

byte[] byteBuf = bitmap2Bytes(src, quality);

if (null == byteBuf) {
return "";
}

String hexBuf = base64.toString(byteBuf);
return hexBuf;
}

/**
* 图片按指定高度等比缩小
* @param map
* @param dstH
* @return
*/
public static Bitmap resizeBitmap(Bitmap map, int dstH) {
if (null == map) {
return null;
}
int width = map.getWidth();
int height = map.getHeight();
int dstHeight = dstH;
int dstWidth = (map.getWidth() / (map.getHeight() / dstHeight));
float scaleWid = ((float) dstWidth) / width;
float scaleHeight = ((float) dstHeight) / height;
Matrix matrix = new Matrix();

matrix.postScale(scaleWid, scaleHeight);
Bitmap resizeMap = Bitmap.createBitmap(map, 0, 0, width, height, matrix, true);
return resizeMap;
}

public static byte[] bitmap2Bytes(Bitmap bm, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (quality < 0) {
quality = 0;
} else if (quality > 100) {
quality = 100;
}
bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
return baos.toByteArray();
}

public static Bitmap bytes2Bimap(final byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}

public static float getDistance(GeoPoint src, GeoPoint dest) {
int lat1 = src.getLatitudeE6();
int lng1 = src.getLongitudeE6();
int lat2 = dest.getLatitudeE6();
int lng2 = dest.getLongitudeE6();
float[] results = new float[3];

Location.distanceBetween(AggTools.latLangFromIntToDouble(lat1),
AggTools.latLangFromIntToDouble(lng1),
AggTools.latLangFromIntToDouble(lat2),
AggTools.latLangFromIntToDouble(lng2),
results);
return results[0];
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: