【问题标题】:android - Why is ImageView canvas black?android - 为什么 ImageView 画布是黑色的?
【发布时间】:2011-05-11 20:40:09
【问题描述】:

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

}

主要活动

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

}

图片

【问题讨论】:

  • 两个cmets:1)你的背景颜色是0x00FF00,这意味着alpha,不透明度,是零。如果您希望背景为绿色,请将背景颜色设置为 0xFF00FF00。
  • 我有点让它像这样工作。在构造函数中,我正在运行 ...setImageBitmap(btm)。并且在 onDraw 中运行 ...canvas.drawBitmap(bitmap, 0,0, null);。这很奇怪,但似乎我必须在构造函数中运行该行,就像启动 imageView(dunno) 一样。但我有两张图片,一张居中,一张位于 0,0 Andy ide?​​span>
  • 2) 它是 ImageView,而不是 Canvas。我只是提出它,因为我认为您的字面意思是画布,并开始考虑到这一点,直到我发现它不是。
  • 是的,对不起,但我在 imageView 的画布上画画。我更新问题头!
  • 谢谢大家!得到它的工作将在明天发布答案。这里有一个新的后续问题stackoverflow

标签: android canvas imageview


【解决方案1】:

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

super.onDraw(canvas);

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

【讨论】:

  • 感谢我添加了 super.onDraw(canvas);
猜你喜欢
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多