我现在做一个功能,吧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>
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>
解决方案 »
- 求助:android怎样实现静默卸载?
- Android 使用poi3.8操作word文档 Help!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
- 新手请教简单问题 onclick没反应..
- native 方法的源码问题。
- 请问如何在JNI层利用Opengl ES显示YUV数据呢?
- Android 开发图片压缩(缩略图)方法
- main.xml中无法添加控件
- android 如何读取指定目录,新手提问。
- 请问,这个错误如何解决?
- Android中Fatal signal 11 (SIGSEGV) at 0x6df6d7bf (code=2), thread 31535
- Proxy.PROXY_CHANGE_ACTION
- NDK中能编译汇编吗?
Bitmap tmpBmp = Bitmap.createBitmap(300,300,Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(tmpBmp);
resView.draw(mCanvas);
canvas.drawBitmap(tmpBmp, 0,0, null);}你这个里面什么都没有画,当然是空白了
做好了翻页,你改改就行了。下载要资源分的。
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);
}
然后在构造函数里提供数据接口传进去进行了。最后通过一个显示的布局
将你这个定义的布局加进去,layout.add(view)就ok了。
Drawable.createFromXml();
这一局之前调用frameLayout.removeAllViews(),不然会有错。。
另外 ,你获得view或者其他控件之类的最好都使用LayoutInflate,而且使用SDK推荐的用法。要不你会出一些莫名其妙的错误,比如之前还是 可以显示的 ,在启动模拟器就显示不了了。。我经常遇到这样的问题,大致会报空指针,或者找不到资源,或者无法启动什么的。。不报错的话 很有可能是你封装view除了问题。
setContentView也要注意,里面最好是一个view,这个view你可以打印一下,
具体实现看一下Drawable类里面的实现吧