2017-05-10 4 views
0

問題を解決する方法の日を探していて、本当に答えが見つかりませんでした。すでに似たような疑問がある場合は事前にお詫びします。Android:画像リソースに書き込み、同じサイズを別のデバイスに保存する

私の問題は簡単です。私はユーザーに名前を入力させたい、そしてこの名前はある種の小さなボックスで画像に書き込まれます。 (画像は意図で共有され、すべてのものはとにかく...)

画像は私のドロウアブルフォルダ内のpngです。だから、どんな種類のデバイスでも常に同じサイズのピクセルでなければなりません。 ビットマップにpngを読み込み、そのビットマップにキャンバスを使用してペイントを作成し、myPaint.setTextSize(100)(setTextSizeはピクセルの場合は100ピクセルです)、drawTextをペイントを使用して同じテキストサイズにしますピクセル)を使用しています。

「MyName」を1つのデバイスに書き込むことは完全には適合しますが、小さい方のデバイスでは外に出て、大きなデバイスでも出てきます。

私はレイアウトについて話しているわけではないので、何も変わらないことを知っていたとしても、固定サイズの描画可能なPNGを使用しようとしました。

私はテキストなどの位置に問題はありません。それだけの大きさ。

とにかく、私はこのテキストサイズの変更がどこから来るのか分かりません。たぶん私は何らかの細部の世話をしていないでしょう。私はあなたが私を助けてくれることを願っています。

    Context context = getBaseContext(); 
        Resources resources = context.getResources(); 
        Bitmap card = BitmapFactory.decodeResource(resources, R.drawable.card); 

        android.graphics.Bitmap.Config bitmapConfig = card.getConfig(); 
        if(bitmapConfig == null) { 
         bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; 
        } 
        // Converting to mutable 
        card = card.copy(bitmapConfig, true); 

        Canvas canvas = new Canvas(card); 
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
        // text color 
        paint.setColor(Color.WHITE); 
        paint.setFakeBoldText(true); 
        paint.setTextAlign(Paint.Align.CENTER); 
        // text size in pixels 
        paint.setTextSize(100); 

        // Name Position 
        float xName = (float) (0.744 * card.getWidth()); 
        float yName = (float) (0.680424 * (card.getHeight() - (paint.ascent()/2))); 
        String name = editName.getText().toString(); 
        canvas.drawText(name, xName, yName, paint); 
+0

http://stackoverflow.com/a/14543066/2931489 –

答えて

0

これを試してください。

/** 
    * Sets the text size for a Paint object so a given string of text will be a 
    * given width. 
    * 
    * @param paint 
    *   the Paint to set the text size for 
    * @param desiredWidth 
    *   the desired width 
    * @param text 
    *   the text that should be that width 
    */ 
    private static void setTextSizeForWidth(Paint paint, float desiredWidth, 
      String text) { 

     // Pick a reasonably large value for the test. Larger values produce 
     // more accurate results, but may cause problems with hardware 
     // acceleration. But there are workarounds for that, too; refer to 
     // http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache 
     final float testTextSize = 48f; 

     // Get the bounds of the text, using our testTextSize. 
     paint.setTextSize(testTextSize); 
     Rect bounds = new Rect(); 
     paint.getTextBounds(text, 0, text.length(), bounds); 

     // Calculate the desired size as a proportion of our testTextSize. 
     float desiredTextSize = testTextSize * desiredWidth/bounds.width(); 

     // Set the paint for that size. 
     paint.setTextSize(desiredTextSize); 
    } 
+0

ないねえ、答えてくれてありがとう。このコードは、より長い名前がボックスに収まるようにテキストのサイズを変更するのに便利で、代替ソリューションになる可能性があります。しかし、完璧なのは、異なるデバイスで同じ長さの2つの全く同じテキストがイメージ上で異なって描画されるという事実に対する解決策です。 –

0

私は密度を使用してこの問題を解決することができました。実際にそれがどのように動作するかを確認しますが、それ仕事はそうします...

Resources resources = context.getResources(); 
float scale = resources.getDisplayMetrics().density; 
paint.setTextSize(30 * scale); 
関連する問題