【问题标题】:Incremental Opacity, wanting constant opacity Image View with DrawableIncremental Opacity,想要恒定不透明度的 Image View with Drawable
【发布时间】:2016-03-31 13:54:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: java android paint android-drawable alpha


    【解决方案1】:

    对我来说,模拟器似乎有更多问题。您也可以尝试使用

    禁用硬件加速
     ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    

    请记住,您不需要每次都实例化一个新的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);
    

    当你按下按钮时

    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);
    }
    

    结果是

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 2014-10-17
      相关资源
      最近更新 更多