我现在做一个功能,吧open.xml转化为图片(Bitmap),我的思路是先把open.xml转化为view,再把view转化为图片(Bitmap),然后进行剪切图片进行显示,可是为什么我运行出来的是空白的页面
OpenTest.java的代码:
package com.OpenTest;
import android.app.Activity;
import android.os.Bundle;public class OpenTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OpenView openView=new OpenView(this);
        setContentView(openView);
    }
}
OpenView.java的代码:
package com.OpenTest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;public class OpenView extends View{
View resView;
public OpenView(Context context) {
super(context);
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
resView = layoutInflater.inflate(R.layout.open, null);
}@Overridepublic void onDraw(Canvas canvas){
Bitmap tmpBmp = Bitmap.createBitmap(300,300,Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(tmpBmp);
resView.draw(mCanvas);
canvas.drawBitmap(tmpBmp, 0,0, null);}
}
open.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">
  <ImageView android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:src="@drawable/p1"></ImageView>
  <Button android:layout_width="match_parent"
  android:layout_height="wrap_content" android:text="smit"></Button>
</LinearLayout>

解决方案 »

  1.   

    public void onDraw(Canvas canvas){
    Bitmap tmpBmp = Bitmap.createBitmap(300,300,Bitmap.Config.ARGB_8888);
    Canvas mCanvas = new Canvas(tmpBmp);
    resView.draw(mCanvas);
    canvas.drawBitmap(tmpBmp, 0,0, null);}你这个里面什么都没有画,当然是空白了
      

  2.   

    额..那怎么把resView画到里面呢??
      

  3.   

    先将xml布局显示出来,然后通过布局id获取布局,然后将布局画入你的截图方法里。就行了。
      

  4.   

    恩,谢谢楼上各位了,我先试试wangrishen123的方法
      

  5.   

    http://wangrishen123.download.csdn.net/
    做好了翻页,你改改就行了。下载要资源分的。
      

  6.   

    wangrishen123 请教下你 我要先显示open.xml,那如何让open.xml里面的Imageview在代码里面动态添加呢?这是我的部分代码:
    public class OpenTest extends Activity {
        View firstvView,secondView,thirdView;
        FrameLayout frameLayout;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            frameLayout=(FrameLayout)findViewById(R.id.OpenLayout);
            LayoutInflater layoutInflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            firstvView=layoutInflater.inflate(R.layout.open, null);
            frameLayout.addView(firstvView);
    }
      

  7.   

    你可以写一个类extends 布局,然后再布局里写空件,比如imageview.
    然后在构造函数里提供数据接口传进去进行了。最后通过一个显示的布局
    将你这个定义的布局加进去,layout.add(view)就ok了。
      

  8.   

    你可以试试直接用Drawable的函数
    Drawable.createFromXml();
      

  9.   

    Drawable.createFromXml()??没见过这个方法啊,能详细说说吗?网上也没找到这个函数的资料
      

  10.   

    frameLayout.addView(firstvView);
    这一局之前调用frameLayout.removeAllViews(),不然会有错。。
    另外 ,你获得view或者其他控件之类的最好都使用LayoutInflate,而且使用SDK推荐的用法。要不你会出一些莫名其妙的错误,比如之前还是 可以显示的 ,在启动模拟器就显示不了了。。我经常遇到这样的问题,大致会报空指针,或者找不到资源,或者无法启动什么的。。不报错的话 很有可能是你封装view除了问题。
    setContentView也要注意,里面最好是一个view,这个view你可以打印一下,
      

  11.   

    直接看源码吧 实际上在load资源的XML文件的时候 到最后获取资源图片返回的drawable都是用的createFromXml函数
    具体实现看一下Drawable类里面的实现吧