找了半天终于可以用了,终于转换成功了,由于要用来加密使用,所以现将要用到的图片转成了字符串,在用字符串转成图片,确切的说是Bitmap类型的,因为是新手,所一说法不是很规范,将其画出来
TestdActivity.java//这是主程序
public class TestdActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
GameView.java//自定义的GameView类
public class GameView extends View {
private Bitmap [] bitmaps=new Bitmap[9];
public String sdsd=null;
public GameView(Context context) {
super(context);
startBitmap();
}
public GameView(Context context,AttributeSet attributeSet){
super(context,attributeSet);
startBitmap();
}
protected void onDraw(Canvas canvas) {
Bitmap bitmap;
bitmap=stringtoBitmap(bitmaptoString(bitmaps[4]));
Paint nPaint=null;
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, nPaint);//将图片显示在画布上
super.onDraw(canvas);
invalidate();
}
private void startBitmap(){//初始化我的bitmaps[]
bitmaps[0]=BitmapFactory.decodeResource(getResources(), R.drawable.tu1);
bitmaps[1]=BitmapFactory.decodeResource(getResources(), R.drawable.tu2);
bitmaps[2]=BitmapFactory.decodeResource(getResources(), R.drawable.tu3);
bitmaps[3]=BitmapFactory.decodeResource(getResources(), R.drawable.tu4);
bitmaps[4]=BitmapFactory.decodeResource(getResources(), R.drawable.tu5);
bitmaps[5]=null;
bitmaps[6]=BitmapFactory.decodeResource(getResources(), R.drawable.tu6);
bitmaps[7]=BitmapFactory.decodeResource(getResources(), R.drawable.tu7);
bitmaps[8]=BitmapFactory.decodeResource(getResources(), R.drawable.tu8);
}
public Bitmap stringtoBitmap(String string){
//将字符串转换成Bitmap类型
Bitmap bitmap=null;
try {
byte[]bitmapArray;
bitmapArray=Base64.decode(string, Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
public String bitmaptoString(Bitmap bitmap){
//将Bitmap转换成字符串
String string=null;
ByteArrayOutputStream bStream=new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG,100,bStream);
byte[]bytes=bStream.toByteArray();
string=Base64.encodeToString(bytes,Base64.DEFAULT);
return string;
}
}