2016-04-01 11 views
3

私はここに空白を描いています!ダイヤモンドの形をした背景の中にテキストを残す

私はダイヤモンドの形をした背景を持つテキストビューを持っています。テキストはダイヤモンド内に含まれる必要があります。 このように

textview_with_diamond_shaped_bg

私はこれをどのように行うのですか?テキストビュー内のテキストの境界を定義する方法はありますか?

答えて

1

編集

DiamondTextViewのようなJavaクラスに以下のコードを入れてください:

public class DiamondTextView extends TextView{ 

    public DiamondTextView(Context ctx, AttributeSet attrs) { 
     super(ctx, attrs); 
    } 

    Paint mBorderPaint = new Paint(); 

     @Override 
     protected void onDraw(Canvas canvas) { 

      mPath.moveTo(mWidth/2 , 0); 
      mPath.lineTo(mWidth , mHeight/2); 
      mPath.lineTo(mWidth /2 , mHeight); 
      mPath.lineTo(0 , mHeight/2); 
      mPath.lineTo(mWidth/2 ,0); 

      //setup the paint for fill 
      mBorderPaint.setAlpha(255); 
      mBorderPaint.setColor(mBorderColor); 
      mBorderPaint.setStyle(Paint.Style.FILL); 
      borderPaint.setStrokeWidth(mBorderWidth); 

      //draw it 
      canvas.drawPath(mPath ,mBorderPaint); 

      //setup the paint for stroke 
      mBorderPaint.setAlpha(51); 
      mBorderPaint.setStyle(Paint.Style.STROKE); 

      //draw it again 
      canvas.drawPath(mPath ,mBorderPaint); 

      super.onDraw(canvas); 
     } 
} 

を、あなたがこのTextViewに使用したい場所として、それを呼び出します。詳しくは

<com.erginus.ABC.Commons.DiamondTextView...../> 

をここをクリックしてください:http://developer.android.com/reference/android/graphics/drawable/shapes/PathShape.html

以下のライブラリも使用できます。https://github.com/siyamed/android-shape-imageview

+0

@PhilTheThrillありがとうございます。 –

+1

これは、テキストを限定するのではなく、背景を描画するだけです。 – ElDuderino

+0

@ElDuderinoはい、バックグラウンドとして設定する必要があります。 –

関連する問題