Per prima cosa vorrei dire per favore non contrassegnare questa domanda come duplicata. Si differenzia dagli altri post in un modo critico. Qualcuno sa come posso dividere il colore del frammento in due colori proprio nel mezzo? Grazie a tutti!Come dividere un frammento in Android in due colori
risposta
ho raggiunto questo creando lo sfondo ideale in photoshop, salvandolo come .png
, quindi impostandolo come sfondo dell'attività per quando è necessario. Semplice, pulito, facile, e poiché ho solo bisogno di due colori tutto il tempo, non devo preoccuparmi che l'immagine diventi pixelata.
qui è di esempio per utilizzare gradiente
float in[] = new float[] {0f,1f};
TextView textView= (TextView)findViewById(R.id.tv_test);
Shader shader = new LinearGradient(
0, textView.getTextSize(), textView.getWidth(), textView.getHeight(), new int[]{Color.RED, Color.BLUE},in,
Shader.TileMode.CLAMP);
textView.getPaint().setShader(shader);
Per raggiungere Seconda Semplicemente utente Framelayout xml
<FrameLayout
android:id="@+id/fl_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_test"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="@android:color/white"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_red_dark" />
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_dark" />
</LinearLayout>
<TextView
android:id="@+id/tv_test2"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:text="A"
android:textColor="@android:color/white"
android:textSize="100sp" />
</FrameLayout>
Per Terzo uno si deve scrivere poco e fare un certo lavoro sul livello di pixel
<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H"
android:textColor="@android:color/holo_red_dark"
android:textSize="100sp" />
<FrameLayout
android:id="@+id/fl_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_test"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="@android:color/white"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_red_dark" />
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_dark" />
</LinearLayout>
<TextView
android:id="@+id/tv_test2"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:text="A"
android:textColor="@android:color/white"
android:textSize="100sp" />
</FrameLayout>
<ImageView
android:id="@+id/iv_test"
android:layout_width="200dp"
android:layout_height="200dp" />
public class MainActivity extends AppCompatActivity {
View linearLayout;
View tvTest2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView textView = (TextView) findViewById(R.id.tv_test);
Shader shader = new LinearGradient(
0, textView.getTextSize()/2, textView.getTextSize(), textView.getTextSize()/2,Color.RED, Color.BLUE,
Shader.TileMode.CLAMP);
textView.getPaint().setShader(shader);
linearLayout = findViewById(R.id.ll_test);
tvTest2 = findViewById(R.id.tv_test2);
}
@Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ImageView imageView = (ImageView) findViewById(R.id.iv_test);
Bitmap b1 = getBitmapFromView(linearLayout);
Bitmap b2 = getBitmapFromView(tvTest2);
imageView.setImageBitmap(textEffect(b1, b2));
}
},2000);
}
public Bitmap textEffect(Bitmap image, Bitmap text) {
if (image.getWidth() != text.getWidth() ||
image.getHeight() != text.getHeight()) {
throw new IllegalArgumentException("Dimensions are not the same!");
}
for (int y = 0; y < image.getHeight(); ++y) {
for (int x = 0; x < image.getWidth(); ++x) {
int textPixel = text.getPixel(x, y);
int imagePixl = image.getPixel(x,y);
int red = Color.red(textPixel);
int blue = Color.blue(textPixel);
int green = Color.green(textPixel);
int alpha = Color.alpha(textPixel);
Log.i("TAG", "textEffect: "+x+"-"+y+",-->"+red+","+blue+","+green+","+alpha);
/*Since text color is white so give the color of background to it
* else make it white*/
if (red == 255) {
if (blue == 255) {
if (green == 255) {
image.setPixel(x, y, imagePixl);
}else
{
image.setPixel(x, y, textPixel);
}
}else{
image.setPixel(x, y, textPixel);
}
}else
{
image.setPixel(x, y, textPixel);
}
}
}
return image;
}
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
}
Quello che esattamente vuole raggiungere ??
Primo uno
Secondo uno
Terzo?
Definire un TextView
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="O"
android:textColor="#88022b54"
android:textSize="250sp"
android:textStyle="bold"
android:shadowColor="#ff2e7cca"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1"/>
Poi
Shader shader = new LinearGradient(
0, mytv.getTextSize()/2, mytv.getTextSize(), mytv.getTextSize()/2,
Color.RED, Color.BLUE,
Shader.TileMode.CLAMP);
mytv.getPaint().setShader(shader);
Potrei applicare questo codice a un frammento? – Ethan
Per quanto ne so, nessun problema. Hai visto correre? È quasi quello che volevi, sì? – statosdotcom
No, non posso inviarmi un link? Puoi anche modificare il codice in fragment_standard? Deve essere diviso verticalmente in modo che il rosso sia sulla metà sinistra e il blu sia sulla metà destra – Ethan
Si può fare come questo
String text = "<font color=#cc0029>Erste Farbe</font> <font color=#ffcc00>zweite Farbe</font>";
yourtextview.setText(Html.fromHtml(text));
- 1. Frammento come singleton in Android
- 2. Dividere un ramo git in due rami?
- 3. come getPackageManager in frammento Android
- 4. Dividere un'applicazione Android in due "rami", gratis ea pagamento
- 5. Come dividere un NSArray in due parti uguali?
- 6. Android: Come aggiornare il mio testo Visualizza in un frammento
- 7. Android: pulsante con due colori di sfondo
- 8. Android - Come dividere una vista in 4 parti uguali?
- 9. Come posso aprire un frammento Android in un nuovo processo?
- 10. Come impostare un tag su un frammento in Android
- 11. Quando dividere una vista MVC in due?
- 12. Come dividere/dividere un array in 2 usando php?
- 13. Come cancellare Frammento backstack in Android
- 14. Android: frammento padre di un frammento nidificato
- 15. Posso dividere un oggetto IEnumerable in due in base a un criterio booleano senza due query?
- 16. Come aprire un frammento sul pulsante clic da un frammento in Android
- 17. Dividere un testo in due colonne con PHP
- 18. set list view adapter in un frammento in android
- 19. Come dividere un numero in Clojure?
- 20. Come dividere la stringa tra due separatori in javascript?
- 21. Come dividere due valori dalla stessa colonna in SQL
- 22. Come visualizzare AlertDialog in un frammento?
- 23. Interazioni frammento frammento nidificato Android
- 24. Bordo a due colori
- 25. Accesso Facebook in frammento in Android
- 26. Come dividere una riga Grid in due colonne?
- 27. Esiste un metodo come setResult() in frammento?
- 28. Creazione di un frammento in una finestra popup - in Android
- 29. Transizione elemento condiviso Android tra un'attività con un frammento in un'altra attività con un frammento
- 30. Come dividere un array?
@Shark Non credo che le Spannabili possano essere usate per dividere il rendering di una singola lettera. Puoi cambiare le cose per ogni lettera, ma non è quello che l'OP chiedeva. –
@DougStevenson abbastanza giusto, non ho davvero capito chiaramente cosa l'OP chiedesse. Ancora non ... Oh, penso di averlo capito ora. Esempio: vuole che una lettera "A" di colore rosso-nero sia di colore rosso nella metà sinistra e che il colore nero sia nella metà destra. – Shark
Siamo sicuri che sia quello che ha chiesto l'OP? "dividi il colore della vista del testo" mi dice che è lo sfondo della textview che desidera dividere in due, non il rendering della lettera. Potrebbe voler dire questo, ma non è chiaro. OPERAZIONE? –