繁体   English   中英

Android:在视图画布上绘图不起作用

[英]Android: drawing on views canvas doesn't work

我正在尝试在所有按钮下的画布上绘制形状。 这是代码:

 Paint paint = new Paint(); paint.setColor(R.color.Kolor); View view; LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.menu_glowne, null); Canvas can = new Canvas(Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)); can.drawRect(0, 0, 200, 200, paint); setContentView(view); view.draw(can); 

不知道为什么我仍然得到下面没有任何内容的布局。 关于我在做什么错的任何想法吗?

提前感谢!

您不会在android中以这种方式绘制。 一切都通过方法重写(onDraw)发生。 请按照本教程入门: http : //www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-1

问候,斯特凡

在您的示例中,您是在画布上而不是在视图的画布上绘制视图。 您应该使用一种简单的方法,不要膨胀您的布局,而是正常加载它,然后找到根容器并设置其背景。 像这样:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Paint paint = new Paint();
    paint.setColor(Color.MAGENTA);
    Bitmap bgr = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); 
    Canvas can = new Canvas(bgr);  
    can.drawRect(50, 50, 200, 200, paint);  
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 
    ll.setBackgroundDrawable(new BitmapDrawable(bgr));    

}

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/ll">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM