View in Android taking the whole screen- how to tell the canvas to respect the limits of the view?

你离开我真会死。 提交于 2019-12-14 02:32:00

问题


EDIT:

OK, so I've extended the lenght and the width of the view to a big part of the screen I can assume that the onDraw() method is NOT TAKING the limits of the view, because it goes through all the screen as I can suppose. So the question now would be how to tell the canvas to respect the limits of the view? Any ideas, maybe forcing some parameters?

END OF EDIT

I have implemented some code for drawing a custom view, but the canvas is blank. I am sure it is running, because I have added log traces, and those are showing up just fine.

How do I properly implement drawing for custom views, and what is wrong with what I've done below?

My custom view that goes inside a layout like this:

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

<pis.core.JMI_VistaMenuIn android:layout_height="150dip" android:layout_width="150dip" android:layout_alignParentTop="true" android:id="@+id/viewAnd01" ></pis.core.JMI_VistaMenuIn>

<Button android:layout_width="wrap_content" android:id="@+id/btnJugar" android:layout_height="wrap_content" android:text="Jugar" android:layout_below="@+id/viewAnd01" android:layout_alignLeft="@+id/viewAnd01" android:layout_alignRight="@+id/viewAnd01"></Button>

</RelativeLayout> 

The customview is the one called: pis.core.JMI_VistaMenuIn. In the preview I got an "java.lang.UnsupportedOperationException" but when the .apk is loaded the xml layout WORKS.

Here is the code for my custom view. As I said before, the lines that contain the logging are getting hit, but my canvas is blank:

public class JMI_VistaMenuIn extends View {

public static final String QUOTE = "Mind Logic+";

public Animation anim;

//Default constructors

public void createAnim(Canvas canvas) {
    Log.i("SC", "Exe canvas 01");
    anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
            .getHeight() / 2);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(10000L);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());

    startAnimation(anim);
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // creates the animation the first time
    if (anim == null) {
        createAnim(canvas);
    }

    Path circle = new Path();

    int centerX = canvas.getWidth() / 2;
    int centerY = canvas.getHeight() / 2;
    int r = Math.min(centerX, centerY);

    circle.addCircle(centerX, centerY, r, Direction.CW);
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setTextSize(30);
    paint.setAntiAlias(true);

    canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
    Log.i("SC", "Exe canvas 02");
}
}

回答1:


Similar code works just fine for me, including the animation. You have another issue somewhere else in your code: try setting the paint style.

Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setTextSize(30);
    paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);


来源:https://stackoverflow.com/questions/6686291/view-in-android-taking-the-whole-screen-how-to-tell-the-canvas-to-respect-the-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!