在画布上绘制位图和椭圆

时间:2014-03-04 23:11:37

标签: java android eclipse canvas bitmap

我正在尝试以这种方式创建用户视图:

位图A作为背景(“hal9000rim”)

背景上的椭圆形B(在画布上设计)

Oval上的位图C(“reflBitmap”)

以下是View Class的代码:

public class HalView extends View {

    final Bitmap reflBitmap;
    final Paint corePaint;
    RectF coreRectf, reflRectf;

    public HalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        reflBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.hal9000refl);
        coreRectf=new RectF(0.2f,0.2f,0.8f,0.8f);

        corePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        corePaint.setStyle(Style.FILL);
        corePaint.setColor(Color.CYAN);

        //corePaint.setShader(new RadialGradient(0.5f, 0.5f, coreRectf.width() / 2, new int[]{Color.rgb(50, 132, 206), Color.rgb(36, 89, 162),
        //    Color.rgb(27, 59, 131)}, new float[]{0.5f, 0.96f, 0.99f}, TileMode.MIRROR));

        setBackgroundResource(R.drawable.hal9000rim);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        reflRectf=new RectF(0, 0, getWidth(),getHeight());
    }


    /* (non-Javadoc)
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.drawOval(coreRectf, corePaint);
        canvas.drawOval(new RectF(0.0f, 0.0f, 400.0f, 400.0f), corePaint);
        //canvas.drawRect(0f, 0f, 100f, 100f, corePaint);
        canvas.drawBitmap(reflBitmap, null,reflRectf, null);
    }


    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {

        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        final int chosenWidth = chooseDimension(widthMode, widthSize);
        final int chosenHeight = chooseDimension(heightMode, heightSize);

        final int side=Math.min(chosenWidth,chosenHeight);

        setMeasuredDimension(side,side);

    }

    private int chooseDimension(final int mode, final int size) {
        switch (mode) {
        case View.MeasureSpec.AT_MOST:
        case View.MeasureSpec.EXACTLY:
            return size;
        case View.MeasureSpec.UNSPECIFIED:
        default:
            return getDefaultDimension();
        }
    }

    private int getDefaultDimension() {
        return 50;
    }}

我有两个问题: 第一个是布局膨胀时出错,这是XML:

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

    <org.codeandmagic.android.gauge.HalView
        android:id="@+id/halView1"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/halView1"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="60dp"
        android:layout_marginRight="240dp"
        android:text="Button" />

</RelativeLayout>

这是错误:

03-04 23:54:52.417:E / AndroidRuntime(26490):java.lang.RuntimeException:无法启动活动ComponentInfo {org.codeandmagic.android.gauge.demo / org.codeandmagic.android.gauge.demo .HalActivity}:android.view.InflateException:二进制XML文件行#6:错误膨胀类org.codeandmagic.android.gauge.HalView

第二个问题是,当我尝试使用RadialGradient代替简单的CYAN颜色时,我会在Eclipse布局中预览此错误:

java.lang.ArrayIndexOutOfBoundsException:3

但渐变中的颜色和位置数组似乎很适合我。 当然,我无法在设备上启动应用程序。

我做错了什么? 感谢

0 个答案:

没有答案