gpt4 book ai didi

java - Android 应用程序 - 从 Activity 绘制位图

转载 作者:行者123 更新时间:2023-12-01 14:37:55 25 4
gpt4 key购买 nike

我正在尝试在屏幕上绘制一个简单的 .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。

最佳答案

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

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);
}

}

快照结果

enter image description here

关于java - Android 应用程序 - 从 Activity 绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16307725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com