Android - почему холст ImageView черный? - PullRequest
0 голосов
/ 12 мая 2011

Я новичок в графике 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);
}

}

Изображение

enter image description here

1 Ответ

2 голосов
/ 12 мая 2011

Глядя на ваш код, вы вряд ли увидите круг как попытку нарисовать его нулевым объектом рисования.Чтобы проверить свой код, убедитесь, что вы используете объект Paint с цветом, контрастирующим с цветом фона.Во-вторых, я бы вызвал метод суперобъекта, например

super.onDraw(canvas);

Я не уверен на 100% в рисовании растрового изображения с нулевым объектом Paint.Также проверьте, что имя пакета вашего класса DrawView (com.hasse.move.DrawView) в файле макета совпадает с именем фактического класса DrawView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...