0

我正在尝试在屏幕上绘制一个简单的 .png 文件。我正在使用以下代码:

public class EditMindMapActivity extends Activity {

    private Button HomeButton;
    private Button SaveButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_mind_map);

        //creating dummy array:
        List<String> mindMapArrayListLabels = new ArrayList<String>();
        int mindMapArrayListImgs[] = new int[100]; //length 100 is arbitrary

        mindMapArrayListLabels.add("Schedule");
        mindMapArrayListImgs[0] = R.drawable.pyr01;
        mindMapArrayListLabels.add("Stuff01");
        mindMapArrayListImgs[1] = R.drawable.tube01;
        mindMapArrayListLabels.add("Stuff02");
        mindMapArrayListImgs[2] = R.drawable.cone01;

        //draw the first image to screen
        View view01 = new View(this); 
        //find relative layout, attach view to it.
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout01);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
        relativeLayout.addView(view01,lp);
        Canvas canvas01 = new Canvas();
        canvas01.drawBitmap(BitmapFactory.decodeResource(getResources(), mindMapArrayListImgs[0]), 10, 10, null);
        view01.draw(canvas01);
    }
}

以上没有做任何事情 - 屏幕显示为空白。我想在屏幕上显示图像 R.drawable.pyr01。

4

1 回答 1

1

创建自定义视图并绘制图像。将自定义视图作为孩子添加到您的相对布局中。根据您的需要修改以下内容。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/rl"
tools:context=".MainActivity" >

    <Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button" />

   </RelativeLayout>

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl);
    MyView mv = new MyView(this);
    relativeLayout.addView(mv);
}

class MyView extends View
{

    public MyView(Context context) {
        super(context);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.afor), 100 ,100, null);
    }

}

结果快照

在此处输入图像描述

于 2013-04-30T20:19:08.367 回答