增量不透明度,需要恒定的不透明度图像视图与Drawable

Yve*_*omb 12 java android alpha paint android-drawable

我以编程方式创建每个视图之间带有水平线的文本视图.使用以编程方式创建的drawable.

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

我已经记录了所提供的两种方法中所有点的可绘制,绘画,图像视图和线性布局的不透明度(getAlpha()),并且从drawable中它总是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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Bla*_*elt 11

对我来说,模拟器看起来更像一些问题.您也可以尝试禁用硬件加速

 ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Run Code Online (Sandbox Code Playgroud)

请记住,ImageView每次都不需要实例化一个新的 ,只是为了在TextViews 之间绘制一个分隔符.你可以使用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);
Run Code Online (Sandbox Code Playgroud)

当你按下按钮

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);
}
Run Code Online (Sandbox Code Playgroud)

结果是

在此输入图像描述