我正在为 LinearLayouts 背景使用 ShapeDrawable。形状是由 cod 制作的,因为我需要根据条件动态地为它们分配颜色。 这是我的自定义形状
public class CustomShapeDrawable extends ShapeDrawable {
private final Paint fillpaint, strokepaint, linePaint = new Paint();
private int strokeWidth = 3;
private final boolean disableBottomBorder;
public CustomShapeDrawable(Shape s, int fill, int stroke, int strokewidth, boolean disablebottomborder) {
super(s);
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(strokewidth);
strokepaint.setColor(stroke);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(strokewidth + 1);
linePaint.setColor(fill);
strokeWidth = strokewidth;
disableBottomBorder = disablebottomborder;
}
public CustomShapeDrawable(Shape s, int fill, int stroke, boolean disablebottomborder) {
super(s);
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(strokeWidth);
strokepaint.setColor(stroke);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(strokeWidth + 1);
linePaint.setColor(fill);
disableBottomBorder = disablebottomborder;
}
@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
shape.resize(canvas.getClipBounds().right, canvas.getClipBounds().bottom);
shape.draw(canvas, fillpaint);
Matrix matrix = new Matrix();
matrix.setRectToRect(new RectF(0, 0, canvas.getClipBounds().right, canvas.getClipBounds().bottom), new RectF(strokeWidth / 2, strokeWidth / 2, canvas.getClipBounds().right - strokeWidth / 2,
canvas.getClipBounds().bottom - strokeWidth / 2), Matrix.ScaleToFit.FILL);
canvas.concat(matrix);
shape.draw(canvas, strokepaint);
if (disableBottomBorder) {
canvas.drawLine(0 + strokeWidth/2, shape.getHeight(), shape.getWidth() - strokeWidth/2, shape.getHeight(), linePaint);
}
}
此 CustomShapeDrawable 用作我的布局的 StateListDrawable,如下所示:
RoundRectShape shapeTopCorners = new RoundRectShape(new float[] { 10, 10, 10, 10, 0, 0, 0, 0 }, null, null);
ShapeDrawable shapeTopCornersNormal =
new CustomShapeDrawable(shapeTopCorners, Global.getFleet().getSkins().getBackgroundcolour(), context.getResources().getColor(R.color.item_line), true);
ShapeDrawable shapeTopCornersPressed =
new CustomShapeDrawable(shapeTopCorners, context.getResources().getColor(R.color.menu_grey), context.getResources().getColor(R.color.item_line), true);
StateListDrawable stateTopCornersRounded = new StateListDrawable();
stateTopCornersRounded.addState(new int[] { android.R.attr.state_focused }, shapeTopCornersPressed);
stateTopCornersRounded.addState(new int[] { android.R.attr.state_pressed }, shapeTopCornersPressed);
stateTopCornersRounded.addState(new int[] {}, shapeTopCornersNormal);
一切看起来都很好,布局与我想要的颜色具有相同的形状。当我在屏幕上看到另一个元素(如键盘或 AlertDialog)时,丑陋的事情就发生了。当我的应用程序再次获得焦点时,布局因随机线条和伪影而变得疯狂。 这就是我的意思:
我该怎么做才能防止或修复这些看起来很丑陋的瑕疵。我不知道为什么首先会发生这种情况。感谢您为我提供的任何帮助。
最佳答案
没有任何建议,目前唯一能解决问题的是: - 我在我的 Activity 中创建了一个名为 invalidate() 的方法,其中对于我想要刷新的所有布局,我添加了 layoutId.invalidate() - 每当显示警告对话框时,调用 invalidate() - 对于所有 EditTexts onFocusChanged 或 onTextChanged,调用 invalidate()
这不是一个完全符合人体工程学的解决方案,但它现在似乎可以工作。
关于android - Android 中 ShapeDrawable 的恼人行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10330509/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
两个gsub产生不同的结果。谁能解释一下为什么?代码也可在https://gist.github.com/franklsf95/6c0f8938f28706b5644d获得.ver=9999str="\tCFBundleDevelopmentRegion\n\ten\n\tCFBundleVersion\n\t0.1.190\n\tAppID\n\t000000000000000"putsstr.gsub/(CFBundleVersion\n\t.*\.).*()/,"#{$1}#{ver}#{$2}"puts'--------'putsstr.gsub/(CFBundleVersio
我在一段非常简单的代码(如我所想)中得到了一个错误的值:org=4caseorgwhenorg=4val='H'endputsval=>nil请不要生气,我希望我错过了一些非常明显的东西,但我真的想不通。谢谢。 最佳答案 这是典型的Ruby错误。case有两种被调用的方法,一种是你传递一个东西作为分支的基础,另一种是你不传递的东西。如果您确实在case中指定了一个表达式语句然后评估所有其他条件并与===进行比较.在这种情况下org评估为false和org===false显然不是真的。所有其他情况也是如此,它们要么是真的,要么是假的。
假设您在Ruby中执行此操作:ar=[1,2]x,y=ar然后,x==1和y==2。是否有一种方法可以在我自己的类中定义,从而产生相同的效果?例如rb=AllYourCode.newx,y=rb到目前为止,对于这样的赋值,我所能做的就是使x==rb和y=nil。Python有这样一个特性:>>>classFoo:...def__iter__(self):...returniter([1,2])...>>>x,y=Foo()>>>x1>>>y2 最佳答案 是的。定义#to_ary。这将使您的对象被视为要分配的数组。irb>o=Obje
我经常将预配置的lambda插入可枚举的方法中,例如“map”、“select”等。但是“注入(inject)”的行为似乎有所不同。例如与mult4=lambda{|item|item*4}然后(5..10).map&mult4给我[20,24,28,32,36,40]但是,如果我制作一个2参数lambda用于像这样的注入(inject),multL=lambda{|product,n|product*n}我想说(5..10).inject(2)&multL因为“inject”有一个可选的单个初始值参数,但这给了我......irb(main):027:0>(5..10).inject
defreverse(ary)result=[]forresult[0,0]inaryendresultendassert_equal["baz","bar","foo"],reverse(["foo","bar","baz"])这行得通,我想了解原因。有什么解释吗? 最佳答案 如果我使用each而不是for/in重写它,它看起来像这样:defreverse(ary)result=[]#forresult[0,0]inaryary.eachdo|item|result[0,0]=itemendresultendforainb基本上就
以下测试中的第3个失败:specify{(0.6*2).shouldeql(1.2)}specify{(0.3*3).shouldeql(0.3*3)}specify{(0.3*3).shouldeql(0.9)}#thisonefails这是为什么呢?这是浮点问题还是ruby或rspec问题? 最佳答案 从rspec-2.1开始specify{(0.6*2).shouldbe_within(0.01).of(1.2)}在那之前:specify{(0.6*2).shouldbe_close(1.2,0.01)}
String#match和Regexp#match在匹配成功时返回一个MatchData:"".match(//)#=>#//.match("")#=>#//.match(:"")#=>#但是Symbol#match返回匹配位置(如String#=~)::"".match(//)#=>0为什么Symbol#match表现不同?有用例吗? 最佳答案 我将其报告为Ruby核心中的错误:https://bugs.ruby-lang.org/issues/11991.让我们看看他们会怎么说。更新被质疑的行为似乎是一个错误。似乎从Ruby2.
在为提供json的Rails3.0.3应用构建API时,发生了一些意外行为。以下是Controller。问题是关于respond_with。我已经在应用程序Controller中有respond_to:json。createAction正常运行,创建后数据也被发回。但是更新操作的respond_with不会发回任何数据。响应主体为空白。defcreateline=get_lineinput_header=line.input_headers.create(params[:input_header])respond_with(input_header,:location=>api_v1_