2014-02-27 13 views
7

Wie kann ich fromFegrees, toDegrees und android:color="#000000" programmgesteuert geben?RotateDrawable programmatisch in android

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item > 


     <rotate 
      android:fromDegrees="45" 
      android:toDegrees="45" 
      android:pivotX="-40%" 
      android:pivotY="87%" > 

      <shape 
      android:shape="rectangle" > 
      <stroke android:color="@android:color/transparent" android:width="10dp"/> 
      <solid 
       android:color="#000000" /> 
     </shape> 

     </rotate> 
    </item> 
</layer-list> 

Ich benutze diese xml im Hintergrund der Ansicht.

Ich muss Dreieck programmgesteuert erstellen. Daher muss RotationDrawable programmgesteuert erstellt werden.

Antwort

0

können Sie einen Matrix verwenden Bilder programmatisch drehen:

rotationMatrix = new Matrix(); 
// Set the scale type of your ImageView to "Matrix" 
yourImageView.setScaleType(ScaleType.MATRIX); 

// to set the rotation to a specific degree: 
rotationMatrix.setRotate(degrees); 
// or add some degrees to the current rotation 
rotationMatrix.postRotate(degrees); 

// apply matrix to your ImageView 
yourImageView.setImageMatrix(rotationMatrix); 
+0

ja ich kann, aber ich habe zu erstellen Dreieck programmgesteuert. also muss ich rotationDrawable programmatisch erstellen. –

3

hier ist eine schöne Lösung eine gedrehte ziehbar für eine Imageview für die Umsetzung:

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f); 
anim.setInterpolator(new LinearInterpolator()); 
anim.setRepeatCount(Animation.INFINITE); 
anim.setDuration(3000); 
iv.setAnimation(anim); 
iv.startAnimation(anim); 
+0

überprüfen Sie diese bearbeitete Antwort ... –

0

ich sehr spät kennen, aber für diejenigen, die Suche nach einer Antwort in der Zukunft.

dies Angenommen, Ihr layer-list:

layer_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@android:id/rotate_drawable"> 
     <rotate 
      android:duration="4000" 
      android:fromDegrees="0" 
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:toDegrees="720"> 

      <shape 
       android:innerRadius="20dp" 
       android:shape="ring" 
       android:thickness="4dp" 
       android:useLevel="false"> 
       <size 
        android:width="48dp" 
        android:height="48dp" /> 

       <gradient 
        android:endColor="@android:color/white" 
        android:startColor="#00ffffff" 
        android:type="sweep" 
        android:useLevel="false" /> 
      </shape> 

     </rotate> 
    </item> 
</layer-list> 

So Ihr java Code kann wie unten sein:

final LayerDrawable layerDrawable = 
     (LayerDrawable) getResources().getDrawable(R.drawable.layer_list); 

final RotateDrawable rotateDrawable = 
     (RotateDrawable) layerDrawable.findDrawableByLayerId(R.id.rotate_drawable); 

final ObjectAnimator objectAnimator = ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000) 
     .setDuration(4000); 
objectAnimator.setRepeatCount(ObjectAnimator.INFINITE); 
objectAnimator.setInterpolator(new LinearInterpolator()); 
objectAnimator.start();