2017-02-09 13 views
2

私は四角形のドロウアブルを作成してから回転させて、右上が斜めになるようにしたいと思っています。丸みを帯びたコーナーイメージの上にオーバーレイします。描画可能enter image description hereandroid xmlの斜め矩形ドロアブルを作成する

は、XMLを使用するために期待していますが、それは単に傾斜とエッジ反対側には絶対に触れない

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item > 
    <rotate 
     android:fromDegrees="19.5" 
     android:toDegrees="-19.5" 
     android:pivotX="-50%" 
     android:pivotY="0%" 
     > 
     <shape 
      android:shape="rectangle" > 
      <solid 
       android:color="@android:color/black" /> 
     </shape> 
    </rotate> 
</item> 

形状を描画するカスタムDrawableのを作成しますが、再び私は失敗し、任意の助けが大好きですか?私はあなたがVectorDrawable(thisthisを参照)で必要なものだと思うあなた

@Override 
public void draw(Canvas canvas) { 
int width = this.getBounds().width(); 
int height = this.getBounds().height(); 
double angle = 19.5 * (Math.PI/180.0); 
double offsetX = height * Math.tan(angle); 

Path path = new Path(); 
Paint paint = new Paint(); 
path.moveTo(0, 0); 
path.lineTo(width, 0); 
path.lineTo(width, height); 
path.lineTo((int)offsetX, height); 
path.close(); 

paint.setColor(_color); 
paint.setStyle(Paint.Style.FILL); 
canvas.drawPath(path, paint); 
} 

答えて

0

に感謝します。 SVGをXMLとして表します。あなたが望むほとんどの図形を描くのにそれを使うことができます。ここでは、カスタムCheckBoxのために使用した丸い角の小さな開いたボックスを描画する例を示します。

open_box.xmlは

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:height="20dp" 
    android:width="20dp" 

    android:viewportWidth="400" 
    android:viewportHeight="400"> 

    <!-- the outside box --> 
    <!-- top line & top left corner --> 
    <path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35 " 
     android:strokeColor="#000000" android:strokeWidth="20" /> 

    <!-- left line & bottom left corner --> 
    <path android:pathData="M 27 64 v271 c0 20 15 35 35 35 " 
     android:strokeColor="#000000" android:strokeWidth="20" /> 

    <!-- bottom line & bottom right corner --> 
    <path android:pathData="M 60 370 h275 c20 0 35 -15 35 -35" 
     android:strokeColor="#000000" android:strokeWidth="20" /> 

    <!-- right line & top right corner --> 
    <path android:pathData="M 370 336 v -271 c0 -20 -15 -35 -35 -35" 
     android:strokeColor="#000000" android:strokeWidth="20" /> 
</vector> 

あなたは、他のドロウアブルのようにそれを使用することができます。

ImageView imageView = (ImageView)findViewById(R.id.view_for_open_box); 
imageView.setBackgroundResource(R.drawable.open_box); 

私の例は単純なものですが、これは非常に複雑な形状を描くために使用できます。 API 19 &の前に、互換性の問題を処理する必要があります。それらについて学ぶためのリンクを参照してください。