10
votes

Ajout d'une image en tant que fond de texte de TextView

Récemment, je voulais faire quelque chose d'Artsy avec mon nouveau projet, comme le titre le suggère, ajoutez une image d'arrière-plan en tant que fond de texte, plutôt que la couleur constante typique tout autour. Plus précisément, j'ai une image de gradient pour ce texte.

quelque chose comme ça

 Entrez la description de l'image ici

En règle générale, je m'attendrais à une solution de type de fichier de ressource digue XML, mais une solution simple est grandement appréciée

EDIT:

Merci pour toutes les solutions de shader, mais pour le souci de question, seules les réponses qui fournissent une solution concernant les arrière-plans d'image seront acceptées / données à une prime


4 commentaires

Je n'ai pas essayé, mais Chiu-ki chan a cet exemple de texte de gradient .


Fonctionne assez bien pour un gradient linéaire, merci. Va certainement envisager d'utiliser cela.


Pouvez-vous spécifier ce que vous entendez par "arrière-plans d'image"? Comment est-ce différent d'un bitmap?


File.JPEG >> Fond de l'image


5 Réponses :


6
votes

Vous pouvez appliquer un bitmapshader code> à la peinture code> textview code>.

myTextView.getPaint().setShader(new BitmapShader(myBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));


0 commentaires

5
votes

shader strong> est la base de classe pour des objets qui renvoient des allures horizontales de couleurs lors du dessin. Une sous-classe de Shader est installée dans une peinture à la peinture. ShareShader (shader) em> strong>. Après que n'importe quel objet (autre qu'un bitmap) soit dessiné avec cette peinture obtiendra sa couleur du shader.

        Bitmap bitmapObj  = BitmapFactory.decodeResource(getResources(),R.drawable.your_image);
        Shader shaderObj = new BitmapShader(bitmapObj,Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
        textViewObj.getPaint().setShader(shaderObj);
        textViewObj.setText("hello");


0 commentaires

1
votes

Vous pouvez utiliser YourTextview.getpaint (). SetShader (nouveau lineargradient (...)); >

Il existe deux constructeurs pour linéargradient code>: P >

/** Create a shader that draws a linear gradient along a line.
    @param x0       The x-coordinate for the start of the gradient line
    @param y0       The y-coordinate for the start of the gradient line
    @param x1       The x-coordinate for the end of the gradient line
    @param y1       The y-coordinate for the end of the gradient line
    @param  color0  The color at the start of the gradient line.
    @param  color1  The color at the end of the gradient line.
    @param  tile    The Shader tiling mode
*/
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile)


0 commentaires

1
votes

créer gradient.xml dans un dossier digue. Vous pouvez définir l'angle et le démarrage et la fin de la couleur. Définissez ceci draigable comme arrière-plan xxx


0 commentaires

0
votes

au lieu de donner une charge sur le GPU / système en utilisant des shaders et des produits suggérées par des réponses ci-dessus, vous recommanderait d'utiliser la mise en page relative.

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/your_imageview_id"
        android:layout_width="fill_parent"
        android:layout_height="As_you_want_dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/your_textview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true" />

</RelativeLayout>


0 commentaires