3

Ich habe TextView (mTextOnImage) und ImageView (mImageView).
Ich kombiniere sie mit der Funktion combineImages, aber wenn ich kombiniere, wird die Textgröße geändert.Android: Textgröße geändert, während die Kombination von Text und Bild

//generate bitmap of textView by using getDrawingCache() 
Bitmap bmp = Bitmap.createBitmap(mTextOnImage.getDrawingCache()); 

//getting image as bitmap from image view (to use as background to combine) 
BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable(); 
Bitmap bitmapBackground = drawable.getBitmap(); 

//combining two bitmaps 
Bitmap combined = combineImages(bitmapBackground, bmp); 

Dies ist combineImages Funktion

public Bitmap combineImages(Bitmap background, Bitmap foreground) { 


     Bitmap cs; 
     cs = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.ARGB_8888); 

     //creating canvas by background image's width and height 
     Canvas comboImage = new Canvas(cs); 
     background = Bitmap.createScaledBitmap(background, background.getWidth(), background.getHeight(), true); 

     //Drawing background to canvas 
     comboImage.drawBitmap(background, 0, 0, null); 

     //Drawing foreground (text) to canvas    
     comboImage.drawBitmap(foreground, mTextOnImage.getLeft(),mTextOnImage.getTop(), null); 

     return cs; 
    } 

Bitmap erfolgreich kombiniert, aber die Textgröße geändert wird. Dies ist
, wie ich Textgröße

mTextOnImage.setTextSize(getResources().getDimensionPixelSize(R.dimen.myFontSize)); 

In String-Ressource gesetzt

<resources> 
    <dimen name="myFontSize">40sp</dimen> 
</resources> 

ich das Hintergrundbild von dem Gerät Galerie erhalten, so dass die Auflösung (Bilddimension) unterschiedlich sein kann.
Gibt es irgendeine Berechnung, die ich verpasst habe?

Zusätzlich, TextView (mTextOnImage) ist ziehbar, also möchte ich auch die Position richtig auf die Kombination dieser beiden setzen.

+0

https://stackoverflow.com/questions/3176033/spannablestring-with-image-example –

+1

@Nilu, Vielleicht ist dieses Beispiel, um ein Bild in 'text String' einzufügen.Aber was ich brauche, ist Text und Bild wie ** Overlay ** zu kombinieren. gefällt das [Bild] (https://i.pinimg.com/736x/80/76/d3/8076d33b2194237f8ef486a22b6246f2--quotes-from-albert-einstein-make-mistakes.jpg) – zey

+0

Haben Sie versucht 'comboImage.drawBitmap (Hintergrund, neue Matrix(), null) '? – azizbekian

Antwort

2

Es wäre hilfreich, Ihr Layout XML und ein paar Bilder zu sehen. Da ich diese nicht benötige, schlage ich vor, dass Sie sicherstellen, dass Ihre Bilder bei der Anzeige nicht in der Größe verändert werden.

Aktualisierung: Bevor Sie sich die längere Lösung ansehen, versuchen Sie, die Einstellung für die Textgröße zu ändern. Der Standardwert ist "sp" und Sie verwenden "px".

mTextOnImage.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.myFontSize)) 

Wenn das nicht funktioniert, versuchen Sie Folgendes:

ich Ihren Code nahm und einige Veränderungen vorgenommen, um zu versuchen, das Problem zu reproduzieren. Im Layout zeige ich eine Textansicht (Höhe und Breite = wrap_content) und ein nicht skaliertes Bild an. Unter diesen beiden Ansichten zeige ich die kombinierte Ansicht an. Ich habe den Text der kombinierten Ansicht oben mit einem weißen Hintergrund positioniert, damit ich einen schnellen Vergleich machen konnte. Hier ist das Ergebnis: „Hallo Welt“

enter image description here

Die beiden s mir gleich aussehen. Dies führt zu der Annahme, dass Ihre kombinierte Bildansicht gestreckt oder geschrumpft wird und Ihr Text dadurch seine Größe ändert, da er nur ein Teil des Bildes ist.

Hier ist mein Code, der das obige Bild erzeugt. Das Bild ist nur eine "PNG" Grafik.

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    private TextView mTextOnImage; 
    private ImageView mImageView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mTextOnImage = findViewById(R.id.textOnImage); 
     mImageView = findViewById(R.id.imageView); 
     mTextOnImage.post(new Runnable() { 
      @Override 
      public void run() { 
       //generate bitmap of textView by using getDrawingCache() 
       mTextOnImage.buildDrawingCache(); 
       Bitmap bmp = Bitmap.createBitmap(mTextOnImage.getDrawingCache()); 

//getting image as bitmap from image view (to use as background to combine) 
       mImageView.buildDrawingCache(); 
       BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable(); 
       Bitmap bitmapBackground = drawable.getBitmap(); 

//    Bitmap bitmapBackground = mImageView.getDrawingCache(); 

//combining two bitmaps 
       Bitmap combined = combineImages(bitmapBackground, bmp); 
       ((ImageView) findViewById(R.id.imageCombined)).setImageBitmap(combined); 
      } 
     }); 
    } 

    public Bitmap combineImages(Bitmap background, Bitmap foreground) { 


     Bitmap cs; 
     cs = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.ARGB_8888); 

     //creating canvas by background image's width and height 
     Canvas comboImage = new Canvas(cs); 
     background = Bitmap.createScaledBitmap(background, background.getWidth(), background.getHeight(), true); 

     //Drawing background to canvas 
     comboImage.drawBitmap(background, 0, 0, null); 

     //Drawing foreground (text) to canvas 
//  comboImage.drawBitmap(foreground, mTextOnImage.getLeft(),mTextOnImage.getTop(), null); 
     comboImage.drawBitmap(foreground, (mImageView.getWidth() - foreground.getWidth())/2, 
           0, null); 

     return cs; 
    } 
} 

activity_main.xml

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     app:layout_constraintBottom_toTopOf="@+id/textOnImage" 
     app:layout_constraintHorizontal_bias="0.5" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_chainStyle="packed" 
     app:srcCompat="@drawable/sky" /> 

    <TextView 
     android:id="@+id/textOnImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:text="Hello World!" 
     android:textColor="@android:color/holo_red_dark" 
     android:textSize="40sp" 
     app:layout_constraintBottom_toTopOf="@+id/imageCombined" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/imageView" /> 

    <ImageView 
     android:id="@+id/imageCombined" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/textOnImage" /> 

</android.support.constraint.ConstraintLayout> 
+0

sollte ich 'TypedValue.COMPLEX_UNIT_PX' oder' TypedValue.COMPLEX_UNIT_SP' ???? – zey

+0

@zey 'TypedValue.COMPLEX_UNIT_PX' -' 40sp' wird im Aufruf 'getResources(). GetDimension (R.dimen.myFontSize)' in 'px' konvertiert. – Cheticamp

Verwandte Themen