2015-12-25 29 views
12

Sto creando a livello di codice visualizzazioni di testo con linee orizzontali tra ogni vista. Utilizzando un drawable creato a livello di codice.Opacità incrementale, con opacità costante Visualizzazione immagine con Drawable

Il problema è che l'opacità inizia a lampeggiare e aumenta gradualmente per ogni linea.

Ho registrato l'opacità (getAlpha()) di drawable, paint, image view e layout lineare in tutti i punti nei due metodi forniti e dai drawable è sempre 255 e le viste 1.0. Non capisco perché non si comporti come se fosse vero. Ho anche provato ad impostare Alpha, non fa differenza.

Perché sta facendo questo e come lo risolvo?

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

enter image description here

risposta

11

A me sembra più qualche problema con l'emulatore. Si potrebbe anche provare a disabilitare l'accelerazione hardware con

ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

prega, anche tenere a mente che non è necessario creare un'istanza di un nuovo ImageView ogni volta, solo per tracciare un divisore tra i TextView s. Potresti usare setDivider. Per esempio.

Nella tua 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); 

e quando si preme il pulsante

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

il risultato è

enter image description here

+0

Grazie per questo. Non pensavo all'emulatore. –

+0

Appena testato su un telefono, va bene. Otterrai un premio quando il periodo scadrà, si spera che otterrai più voti. –

+0

siete i benvenuti – Blackbelt