android - 为什么 ImageView 画布是黑色的?

发布于 2024-11-06 19:00:25 字数 2690 浏览 1 评论 0原文

我是 Android 图形新手,所以请坚持住! 无论我做什么,屏幕都是黑的。 我画一个圆圈,画位图并注意显示。 这是 xml、代码和显示我的屏幕的图片。 我想我必须设置 ImageView 的大小,也许这就是它是黑色的原因??!

R.layout.main1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<com.hasse.move.DrawView 
    android:id="@+id/mainView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#00FF00"
    android:adjustViewBounds="true"

  />
   <RelativeLayout 
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >
   <EditText 
    android:id="@+id/edittextaddtext"
    android:layout_width="fill_parent"
    android:layout_toLeftOf="@+id/btnsave"
    android:layout_height="wrap_content"
    android:text="wrap"
   />
   <Button 
    android:id="@+id/btnsave" 
    android:layout_alignParentRight="true" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="save"
   />
   </RelativeLayout>
 </RelativeLayout>

DrawView 类

public class DrawView extends ImageView {
private Context ctx;
public DrawView(Context context, AttributeSet atts) {
    super(context, atts);
    this.ctx = context;
} 
@Override 
protected void onDraw(Canvas canvas) {
    String filePath = Environment.getExternalStorageDirectory().toString() + "/PTPPservice/163693fe-7c48-4568-a082-00047123b9f1.2.IMAG2200.jpg";
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;

    Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);     

    canvas.drawBitmap(bitmap, 10,10, null);
    canvas.drawCircle(10,10,10,null);
}

}

主 Activity

public class Main extends Activity {

DrawView theImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // draw the view
    setContentView(R.layout.main1);

    theImage = (DrawView) findViewById(R.id.mainView);
    //do stuff
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
//  Toast.makeText(this, "onConfigurationChanged",Toast.LENGTH_LONG).show();
    super.onConfigurationChanged(newConfig);
}

}

图像

在此处输入图像描述

I'm new to android graphic so hold on to your hats!
The screen is black whatever i do.
I draw a circle and i draw bitmap and noting shows.
Here is xml,code and a picture showing my screen.
Im thinking i have to set the size of the ImageView maybe that's why it's black??!

R.layout.main1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<com.hasse.move.DrawView 
    android:id="@+id/mainView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#00FF00"
    android:adjustViewBounds="true"

  />
   <RelativeLayout 
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >
   <EditText 
    android:id="@+id/edittextaddtext"
    android:layout_width="fill_parent"
    android:layout_toLeftOf="@+id/btnsave"
    android:layout_height="wrap_content"
    android:text="wrap"
   />
   <Button 
    android:id="@+id/btnsave" 
    android:layout_alignParentRight="true" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="save"
   />
   </RelativeLayout>
 </RelativeLayout>

DrawView class

public class DrawView extends ImageView {
private Context ctx;
public DrawView(Context context, AttributeSet atts) {
    super(context, atts);
    this.ctx = context;
} 
@Override 
protected void onDraw(Canvas canvas) {
    String filePath = Environment.getExternalStorageDirectory().toString() + "/PTPPservice/163693fe-7c48-4568-a082-00047123b9f1.2.IMAG2200.jpg";
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;

    Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);     

    canvas.drawBitmap(bitmap, 10,10, null);
    canvas.drawCircle(10,10,10,null);
}

}

main Activity

public class Main extends Activity {

DrawView theImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // draw the view
    setContentView(R.layout.main1);

    theImage = (DrawView) findViewById(R.id.mainView);
    //do stuff
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
//  Toast.makeText(this, "onConfigurationChanged",Toast.LENGTH_LONG).show();
    super.onConfigurationChanged(newConfig);
}

}

Image

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-11-13 19:00:25

查看您的代码,当您尝试使用空绘画对象绘制它时,您不太可能看到该圆圈。要测试代码,请确保使用颜色与背景颜色形成对比的 Paint 对象。其次,我会调用超级对象方法,例如

super.onDraw(canvas);

我不能 100% 确定是否使用空 Paint 对象绘制位图。
另外,检查布局文件中的 DrawView 类 (com.hasse.move.DrawView) 的包名称与实际的 DrawView 类的包名称是否匹配。

Looking at your code, your unlikely to see the circle as your trying to draw it with a null paint object. To test your code make sure you use a Paint object with a colour that contrasts the background colour. Secondly, I would call the super object method e.g.

super.onDraw(canvas);

I'm not 100% sure about drawing the bitmap with a null Paint object.
Also, check the package name of your DrawView class (com.hasse.move.DrawView) in your layout file matches that of the actual DrawView class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文