如何在Android中使画布透明? [英] How to make canvas transparent in Android?

查看:199
本文介绍了如何在Android中使画布透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的应用程序的绘图表面透明,这样我的应用程序可以看起来像用户可以在我的背景图像上绘制,我有以下XML:

I want to make the drawing surface of my app transparent so that the my app can look like the user can draw on top of my background image, I have the following XML:

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

    <com.almondmendoza.drawings.DrawingSurface
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:id="@+id/drawingSurface"
    />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" >

        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="OK"
                android:onClick="onClick"
                android:id="@+id/colorGreenBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Scratch"
                android:onClick="onClick"
                android:id="@+id/colorRedBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Minor Scratch"
                android:onClick="onClick"
                android:id="@+id/colorBlueBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Dent"
                android:onClick="onClick"
                android:id="@+id/dentBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Minor Dent"
                android:onClick="onClick"
                android:id="@+id/minorDentBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Damaged"
                android:onClick="onClick"
                android:id="@+id/damagedBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Undo"
                android:onClick="onClick"
                android:id="@+id/undoBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Redo"
                android:onClick="onClick"
                android:id="@+id/redoBtn" />
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Save"
                android:onClick="onClick"
                android:id="@+id/saveBtn" />

    </LinearLayout>

</RelativeLayout>

上面的XML代码的输出如下:

The ouput of the XML code above is like this:

>

正如你可以看到的绘图表面是屏幕的黑色部分。我想要这是透明的,是可能吗?

As you can see the drawing surface is the black part of the screen. I want this to be transparent, is that possible?

非常感谢任何帮助! :)

Thanks a lot for any help! :)

推荐答案

package com.logistics.kiddiekuts.Trans;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class TransparentPanel extends RelativeLayout {
    private Paint innerPaint, borderPaint;

    public TransparentPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TransparentPanel(Context context) {
        super(context);
        init();
    }

    private void init() {
        innerPaint = new Paint();
        innerPaint.setARGB(225, 225, 225, 225); // gray
        innerPaint.setAntiAlias(true);

        borderPaint = new Paint();
        borderPaint.setARGB(255, 255, 255, 255);
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Style.STROKE);
        borderPaint.setStrokeWidth(2);
    }

    public void setInnerPaint(Paint innerPaint) {
        this.innerPaint = innerPaint;
    }

    public void setBorderPaint(Paint borderPaint) {
        this.borderPaint = borderPaint;
    }

    protected void dispatchDraw(Canvas canvas) {

        RectF drawRect = new RectF();
        drawRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight());

        canvas.drawRoundRect(drawRect, 8, 8, innerPaint);
        // canvas.drawRoundRect(drawRect, 5, 5, borderPaint);

        super.dispatchDraw(canvas);
    }
}

XML ::

                <TextView android:layout_width="wrap_content"
                    android:text="Name :" android:textColor="#000000"
                    android:layout_marginLeft="15dip" android:layout_marginTop="27dip"
                    android:id="@+id/tvname" android:layout_height="wrap_content"></TextView>
                <EditText android:layout_width="197dip" android:id="@+id/name"
                    android:layout_marginLeft="7dip" android:textSize="12dip"
                    android:layout_marginTop="23dip" android:layout_toRightOf="@+id/tvname"
                    android:layout_height="35dip" />

                <TextView android:layout_width="wrap_content"
                    android:text="Phone :" android:textColor="#000000"
                    android:layout_marginLeft="15dip" android:layout_marginTop="18dip"
                    android:layout_below="@+id/tvname" android:id="@+id/tvphone"
                    android:layout_height="wrap_content"></TextView>
                <EditText android:layout_width="197dip" android:id="@+id/phone"
                    android:textSize="12dip" android:layout_marginLeft="5dip"
                    android:layout_marginTop="3dip" android:layout_toRightOf="@+id/tvphone"
                    android:layout_below="@+id/name" android:layout_height="35dip"></EditText>

                <TextView android:layout_width="wrap_content"
                    android:text="Email :" android:textColor="#000000"
                    android:layout_marginLeft="15dip" android:layout_marginTop="20dip"
                    android:layout_below="@+id/tvphone" android:id="@+id/tvemail"
                    android:layout_height="wrap_content"></TextView>
                <EditText android:layout_width="197dip" android:id="@+id/email"
                    android:textSize="12dip" android:layout_marginLeft="9dip"
                    android:layout_marginTop="3dip" android:layout_toRightOf="@+id/tvemail"
                    android:layout_below="@+id/phone" android:layout_height="35dip"></EditText>


                <TextView android:layout_width="wrap_content"
                    android:text="Category :" android:textColor="#000000"
                    android:layout_marginLeft="15dip" android:layout_marginTop="20dip"
                    android:layout_below="@+id/tvemail" android:id="@+id/tvcategory"
                    android:layout_height="wrap_content"></TextView>

                <TextView android:layout_width="wrap_content" android:id="@+id/category"
                    android:textColor="#000000" android:layout_marginLeft="10dip"
                    android:text="For Appointment" android:layout_marginTop="8dip"
                    android:layout_toRightOf="@+id/tvcategory" android:layout_below="@+id/email"
                    android:layout_height="wrap_content"></TextView>


                <TextView android:layout_width="wrap_content"
                    android:text="Location :" android:textColor="#000000"
                    android:layout_marginLeft="15dip" android:layout_marginTop="10dip"
                    android:layout_below="@+id/tvcategory" android:id="@+id/tvlocation"
                    android:layout_height="wrap_content"></TextView>

                <TextView android:layout_width="wrap_content" android:id="@+id/txtlocation"
                    android:textColor="#000000" android:layout_marginLeft="10dip"
                    android:text="Atlanta,Ga"  android:layout_marginTop="10dip"
                    android:layout_toRightOf="@+id/tvlocation" android:layout_below="@+id/category"
                    android:layout_height="wrap_content"></TextView>


                <TextView android:layout_width="wrap_content"
                    android:textColor="#000000" android:text="Message:"
                    android:layout_marginLeft="15dip" android:layout_marginTop="10dip"
                    android:layout_below="@+id/tvlocation" android:id="@+id/tvatn"
                    android:layout_height="wrap_content"></TextView>

                <EditText android:layout_width="250dip" android:id="@+id/atn"
                    android:textSize="12dip" android:lines="7" android:layout_below="@+id/tvatn"
                    android:layout_marginLeft="15dip" android:layout_marginTop="3dip"
                    android:gravity="top|left" android:layout_height="wrap_content"></EditText>
                <Button android:layout_height="25dip" android:layout_below="@+id/atn"
                    android:layout_marginLeft="15dip" android:layout_marginTop="3dip"
                    android:id="@+id/btnsubmit" android:background="@drawable/submit_btn"


                    android:layout_width="250dip"></Button>
                <TextView android:id="@+id/text" android:layout_width="fill_parent"
                    android:layout_below="@+id/btnsubmit" android:layout_height="wrap_content"></TextView>
            </com.logistics.kiddiekuts.Trans.TransparentPanel>

这篇关于如何在Android中使画布透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆