找了半天终于可以用了,终于转换成功了,由于要用来加密使用,所以现将要用到的图片转成了字符串,在用字符串转成图片,确切的说是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;
    }
    
    
}



更多相关文章

  1. Android(安卓)高清加载长图或大图方案
  2. Bitmap保存图片到指定路径
  3. android 图片下载方法总结
  4. Android(安卓)的WebView长按保存图片
  5. 解决Viewpager满屏不能自适应填充内容的三种办法
  6. Android开发之非Activity类型的Context启动一个目标Activity
  7. 初学Android,字符串,数字,尺寸,数组资源(十二)
  8. android之微信分享图片
  9. Android(安卓)Studio 3.6 Windos、mac最新版安装教程图片

随机推荐

  1. [置顶] 学坏
  2. Android内存泄漏终极解决篇(下)
  3. Android(安卓)6.0(权限申请)反射和注解实现
  4. SD功能移植
  5. Android集成讯飞语音、百度语音、阿里语
  6. android中屏幕适配的方式
  7. android activity 应该知道的一切
  8. Android学习系列(3)--App自动更新之自定
  9. 日积月累:EditText软键盘的显示和隐藏
  10. android 面试 android 知识点 提高篇