문제

화면에 텍스트를 그린 이미지 뷰를 확장하여 사용자 정의 이미지보기를 만듭니다. 그러나 에뮬레이터 화면에 페인트 된 내용이 보이지는 않지만 로그 메시지와 printlns는 로그 콘솔에 인쇄됩니다. 내가 뭔가를하지 않습니까?

이것이 내 활동입니다

public class HelloAndroidActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //        setContentView(R.layout.main);
        CustomImageView myView = new CustomImageView(getApplicationContext());
        System.out.println("Setting the view");
        myView.invalidate();
        setContentView(myView);
        System.out.println("Calling invalidate");
    }
}

이것은 내 CustomImageView입니다

public class CustomImageView extends ImageView
{

    /**
     * @param context
     */
    public CustomImageView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
        setBackgroundColor(0xFFFFFF);
    }

    /**
     * @param context
     * @param attrs
     */
    public CustomImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        // TODO Auto-generated method stub
                super.onDraw(canvas);
        System.out.println("Painting content");
        Paint paint  = new Paint(Paint.LINEAR_TEXT_FLAG);
        paint.setColor(0x0);
        paint.setTextSize(12.0F);
        System.out.println("Drawing text");
        canvas.drawText("Hello World in custom view", 100, 100, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        // TODO Auto-generated method stub
        Log.d("Hello Android", "Got a touch event: " + event.getAction());
        return super.onTouchEvent(event);

    }
}

ontouchevent ()의 로그 메시지조차 인쇄되지만 아무것도 그려지지 않습니다.

레이아웃이있는 Main.xml입니다

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/AbsoluteLayout">
</AbsoluteLayout>
도움이 되었습니까?

해결책

색상 값을 사용하십시오 컬러. 화이트 또는 검정색 헥사 값 대신.

다른 팁

캔버스 크기를 확인 했습니까? 이미지보기는 비트 맵/드로우가 크기를 반환하고 스칼로 타입 플래그를 기반으로 뷰의 크기를 결정할 것으로 기대합니다. 코드에서 레이아웃 요구에 대한보기의 크기를 결정하는 것은 없습니다.

-약간 뒤틀리게 하다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top