我有一个几乎可以工作的实现,这里是代码:
public class MyTextVIEw extends TextVIEw { Paint paint = new Paint(); voID init() { paint.setAlpha(0xffffffff); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY)); paint.setShader( new linearGradIEnt( 0,getWIDth(),getHeight(),0xffffffff,0x00000000,Shader.TileMode.CLAMP)); } voID draw(Canvas canvas) { super.draw(canvas); canvas.drawRect(0,paint); }}
它位于此布局中,具有自己的半透明背景:
<VIEw background="#33000000"> <MyTextVIEw /></VIEw>
绘画模式似乎删除了其父视图的半透明背景颜色.我不确定如何阻止textvIEw类中的绘图代码影响其父视图的背景颜色?
谢谢
解决方法 试试这个:public class AlphaMaskLayout extends relativeLayout { private Paint paint; public AlphaMaskLayout(Context context) { super(context); init(context,null,0); } public AlphaMaskLayout(Context context,AttributeSet attrs) { super(context,attrs); init(context,attrs,AttributeSet attrs,int defStyle) { super(context,defStyle); init(context,defStyle); } private voID init(Context context,int defStyle) { paint = new Paint(Paint.ANTI_AliAS_FLAG); paint.setAlpha(0xffffffff); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); setLayerType(LAYER_TYPE_SOFTWARE,null); setwillNotDraw(false); } @OverrIDe public voID draw(Canvas canvas) { super.draw(canvas); canvas.drawRect(0,paint); } @OverrIDe protected voID onSizeChanged(int w,int h,int olDW,int oldh) { super.onSizeChanged(w,h,olDW,oldh); paint.setShader( new linearGradIEnt( 0,w,new int[]{ 0xffffffff,0xfaffffff,0xffffffff},new float[]{0,0.15f,0.3f,0.7f,0.85f,1f},Shader.TileMode.CLAMP)); }}
在xml中:
<your.package.AlphaMaskLayout androID:layout_centerInParent="true" androID:layout_wIDth="160dp" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/moving_textvIEw" androID:layout_centerVertical="true" androID:padding="16dp" androID:textSize="16sp" androID:background="@null" androID:textcolor="@androID:color/white" androID:shadowcolor="@androID:color/black" androID:shadowDx="1" androID:shadowDy="1" androID:shadowRadius="1" androID:text="Hello World!!!" androID:gravity="center" androID:layout_centerInParent="true" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/></saulmm.coordinatorexamples.AlphaMaskLayout>总结
以上是内存溢出为你收集整理的android – 在TextView上使用alpha掩码也会删除父视图alpha?全部内容,希望文章能够帮你解决android – 在TextView上使用alpha掩码也会删除父视图alpha?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)