增量不透明度,想要恒定不透明度的带有Drawable的图像视图。

12

我正在使用程序编程方式创建带有水平线的文本视图。使用程序创建的可绘制对象。

问题是,不透明度从浅到深逐渐增加。

我已经在两个提供的方法中记录了可绘制对象、画笔、图像视图和线性布局的不透明度 (getAlpha()),并且从可绘制对象中它始终为 255,而从视图中则为 1.0。我不明白为什么它不表现得像这是真的一样。我还尝试过设置 Alpha,但没有任何区别。

为什么会这样做,我该如何解决?

xml:

<LinearLayout android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" .../>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="PaintDashedLines"
        android:text="Press Me"/>
</LinearLayout>

Java:

static int tvCount = 0;

public void PaintDashedLines(View v) {
    LinearLayout ll = (LinearLayout) findViewById(R.id.main);
    TextView tv = new TextView(MainActivity.this);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(25);
    tv.setPadding(0, 5, 0, 5);
    ll.addView(tv);
    tv.setText("TextView " + tvCount);
    ImageView divider = new ImageView(MainActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            ll.getWidth(), 2);
    lp.setMargins(0, 5, 0, 5);
    divider.setLayoutParams(lp);
    divider.setBackground(CreateDashedLined());
    ll.addView(divider);
    tvCount++;
}

public static Drawable CreateDashedLined() {
    ShapeDrawable sd = new ShapeDrawable(new RectShape());
    Paint fgPaintSel = sd.getPaint();
    fgPaintSel.setColor(Color.BLACK);
    fgPaintSel.setStyle(Paint.Style.STROKE);
    fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
    return sd;
}

输入图片说明

1个回答

11

对我来说,似乎更多是模拟器的问题。你也可以尝试使用禁用硬件加速。

 ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
请记住,您无需每次都实例化一个新的 ImageView,只是为了在 TextView 之间绘制分隔符。您可以使用 setDivider。例如,在您的 onCreate 中。
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.setIntrinsicHeight(1);
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);
linearLayout.setDividerDrawable(sd);

当你按下按钮时

public void pressMe(View view) {
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
    TextView tv = new TextView(MainActivity.this);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(25);
    tv.setPadding(0, 5, 0, 5);
    tv.setText("TextView " + linearLayout.getChildCount());
    linearLayout.addView(tv);
}
结果是:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接