如何使画布透明的Android? [英] How to make canvas transparent in Android?
本文介绍了如何使画布透明的Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让我的应用程序的绘图表面透明的,使得我的应用程序可以像用户可以在我的背景图像顶部画,我有以下的XML:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@可绘制/ BG2
>
< com.almondmendoza.drawings.DrawingSurface
机器人:layout_width =FILL_PARENT
机器人:layout_height =200dip
机器人:ID =@ + ID / drawingSurface
/>
<的LinearLayout
机器人:方向=横向
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真正的>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=OK
机器人:的onClick =的onClick
机器人:ID =@ + ID / colorGreenBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=划痕
机器人:的onClick =的onClick
机器人:ID =@ + ID / colorRedBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=小划痕
机器人:的onClick =的onClick
机器人:ID =@ + ID / colorBlueBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=凹痕
机器人:的onClick =的onClick
机器人:ID =@ + ID / dentBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=小凹痕
机器人:的onClick =的onClick
机器人:ID =@ + ID / minorDentBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=损坏
机器人:的onClick =的onClick
机器人:ID =@ + ID / damagedBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=撤消
机器人:的onClick =的onClick
机器人:ID =@ + ID / undoBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=重做
机器人:的onClick =的onClick
机器人:ID =@ + ID / redoBtn/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=保存
机器人:的onClick =的onClick
机器人:ID =@ + ID / saveBtn/>
< / LinearLayout中>
< / RelativeLayout的>
的XML code上面的输出中是这样的:
正如你所看到的画面屏幕的黑色部分。我想这是透明的,这可能吗?
非常感谢您的帮助! :)
解决方案
包com.logistics.kiddiekuts.Trans;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.RectF;
进口android.graphics.Paint.Style;
进口android.util.AttributeSet;
进口android.widget.RelativeLayout;
公共类TransparentPanel扩展RelativeLayout的{
私人油漆innerPaint,borderPaint;
公共TransparentPanel(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
在里面();
}
公共TransparentPanel(上下文的背景下){
超(上下文);
在里面();
}
私人无效的init(){
innerPaint =新的油漆();
innerPaint.setARGB(225,225,225,225); // 灰色
innerPaint.setAntiAlias(真正的);
borderPaint =新的油漆();
borderPaint.setARGB(255,255,255,255);
borderPaint.setAntiAlias(真正的);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
公共无效setInnerPaint(漆innerPaint){
this.innerPaint = innerPaint;
}
公共无效setBorderPaint(漆borderPaint){
this.borderPaint = borderPaint;
}
保护无效dispatchDraw(帆布油画){
RectF的drawRect =新RectF();
drawRect.set(0,0,getMeasuredWidth(),getMeasuredHeight());
canvas.drawRoundRect(drawRect的,8,8,innerPaint);
// canvas.drawRoundRect(drawRect中,5,5,borderPaint);
super.dispatchDraw(画布);
}
}
XML ::
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:文本=名称:Android的:文字颜色=#000000
机器人:layout_marginLeft =15dip机器人:layout_marginTop =27dip
机器人:ID =@ + ID / tvname机器人:layout_height =WRAP_CONTENT>< / TextView的>
< EditText上的android:layout_width =197dip机器人:ID =@ + ID /名称
机器人:layout_marginLeft =7dip机器人:TEXTSIZE =12dip
机器人:layout_marginTop =23dip机器人:layout_toRightOf =@ + ID / tvname
机器人:layout_height =35dip/>
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:文本=电话:机器人:文字颜色=#000000
机器人:layout_marginLeft =15dip机器人:layout_marginTop =18dip
机器人:layout_below =@ + ID / tvname机器人:ID =@ + ID / tvphone
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< EditText上的android:layout_width =197dip机器人:ID =@ + ID /电话
机器人:TEXTSIZE =12dip机器人:layout_marginLeft =5dip
机器人:layout_marginTop =3dip机器人:layout_toRightOf =@ + ID / tvphone
机器人:layout_below =@ + ID /名称机器人:layout_height =35dip>< /的EditText>
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:文本=电子邮件:机器人:文字颜色=#000000
机器人:layout_marginLeft =15dip机器人:layout_marginTop =20dip
机器人:layout_below =@ + ID / tvphone机器人:ID =@ + ID / tvemail
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< EditText上的android:layout_width =197dip机器人:ID =@ +帐号/电邮
机器人:TEXTSIZE =12dip机器人:layout_marginLeft =9dip
机器人:layout_marginTop =3dip机器人:layout_toRightOf =@ + ID / tvemail
机器人:layout_below =@ + ID /电话机器人:layout_height =35dip>< /的EditText>
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:文本=范畴:机器人:文字颜色=#000000
机器人:layout_marginLeft =15dip机器人:layout_marginTop =20dip
机器人:layout_below =@ + ID / tvemail机器人:ID =@ + ID / tvcategory
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< TextView的Android的:layout_width =WRAP_CONTENT机器人:ID =@ + ID /类别
机器人:文字颜色=#000000机器人:layout_marginLeft =10dip
机器人:文本=任命的Android版本:layout_marginTop =8DIP
机器人:layout_toRightOf =@ + ID / tvcategory机器人:layout_below =@ +帐号/电邮
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:文本=的位置:机器人:文字颜色=#000000
机器人:layout_marginLeft =15dip机器人:layout_marginTop =10dip
机器人:layout_below =@ + ID / tvcategory机器人:ID =@ + ID / tvlocation
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< TextView的Android的:layout_width =WRAP_CONTENT机器人:ID =@ + ID / txtlocation
机器人:文字颜色=#000000机器人:layout_marginLeft =10dip
机器人:文本=亚特兰大的Android版本:layout_marginTop =10dip
机器人:layout_toRightOf =@ + ID / tvlocation机器人:layout_below =@ + ID /类别
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:文字颜色=#000000的android:文本=消息:
机器人:layout_marginLeft =15dip机器人:layout_marginTop =10dip
机器人:layout_below =@ + ID / tvlocation机器人:ID =@ + ID / tvatn
机器人:layout_height =WRAP_CONTENT>< / TextView的>
< EditText上的android:layout_width =250dip机器人:ID =@ + ID / ATN
机器人:TEXTSIZE =12dip机器人:行=7安卓layout_below =@ + ID / tvatn
机器人:layout_marginLeft =15dip机器人:layout_marginTop =3dip
机器人:重力=顶|左的Android版本:layout_height =WRAP_CONTENT>< /的EditText>
<按钮机器人:layout_height =25dip机器人:layout_below =@ + ID / ATN
机器人:layout_marginLeft =15dip机器人:layout_marginTop =3dip
机器人:ID =@ + ID / btnSubmit按钮机器人:背景=@可绘制/的submit_btn
机器人:layout_width =250dip>< /按钮>
< TextView的机器人:ID =@ + ID /文本机器人:layout_width =FILL_PARENT
机器人:layout_below =@ + ID / btnSubmit按钮机器人:layout_height =WRAP_CONTENT>< / TextView的>
< /com.logistics.kiddiekuts.Trans.TransparentPanel>
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>
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屋!
查看全文