我现在做一个功能,吧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手机只能在连上豌豆荚的时候才能链接上eclipse 为什么?
- android webview 应用启动太慢
- 请教:QQ游戏大厅中游戏安装在内存卡的原理
- 这个异常怎么解决
- 请问如何在windows下读取安桌手机的型号,读取里面的信息
- android GridView 异步加载 网络图片 怎么用Intent传递一个Bitmap数组(集合也行)?请贴出详细代码谢谢
- android动画双缓冲用法求教
- 求助!!!
- 怎样操作sqlite???????????????????????
- 求大神教怎样编辑一个helloworld的安卓项目啊
- 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类里面的实现吧