Issue
I am programmatically creating textviews with horizontal lines between each view. Using a drawable created programmatically.
The problem is, the opacity starts off light and gradually increases for each line.
I've logged the opacity (getAlpha()) of the drawable, paint, image view and linear layout at all the points in the two methods provided and from the drawables it's always 255 and the views 1.0. I don't understand why it's not behaving as though this is true. I've also tried setting Alpha, it makes no difference.
Why is it doing this and how do I fix it?
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;
}
Solution
To me looks like more some issue with the emulator. You could also try disable the hardware acceleration with
ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Please, keep also in mind that you don't need to instantiate a new ImageView
every time, just to draw a divider between the TextView
s. You could use setDivider. E.g.
In your 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);
and when you press the button
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);
}
the result is
Answered By - Blackbelt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.