增加不透明度,想要恒定的不透明度 Image View with Drawable

Incremental Opacity, wanting constant opacity Image View with Drawable

我正在以编程方式创建文本视图,每个视图之间都有水平线。使用以编程方式创建的可绘制对象。

问题是,不透明度从光线开始逐渐增加。

我已经记录了提供的两种方法中所有点的可绘制对象、绘图、图像视图和线性布局的不透明度 (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;
}

对我来说,模拟器似乎更像是一些问题。您也可以尝试使用

禁用硬件加速
 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);
}

结果是