2015-08-26 104 views
0

我有一個自定義繪製的,我希望把它blink.Currently我可以看到旁邊的文本顯示的可繪製的幸福,但沒有閃爍影響創建自定義繪製閃爍

public class CustomDrawable extends Drawable { 
private Context mContext; 
private int mColor; 

public CustomDrawable(Context context, int color) { 
    mContext = context; 
    mColor = color; 
} 

@Override 
public void draw(Canvas canvas) { 
    int x = 30; 
    int y = 30; 
    int radius = 15; 
    Paint paint = new Paint(); 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(mContext.getResources().getColor(R.color.transparent)); 
    paint.setAntiAlias(true); 
    canvas.drawPaint(paint); 
    paint.setColor(mColor); 
    canvas.drawCircle(x/2, y/2, radius, paint); 
} 

@Override 
public void setAlpha(int alpha) { 

} 

@Override 
public void setColorFilter(ColorFilter cf) { 

} 

@Override 
public int getOpacity() { 
    return 0; 
} 

}

如何我可以使自定義可繪製,使其閃爍我試着改變alpha,但沒有影響。

mTextView = (TextView) findViewById(R.id.textView); 
    AnimationDrawable drwable = new AnimationDrawable(); 
    drwable.addFrame(new CustomDrawable(this, Color.RED), 50); 
    drwable.setAlpha(255); 
    drwable.setEnterFadeDuration(500); 
    mTextView.setCompoundDrawables(null, drwable, null, null); 
    drwable.start() 
+0

http://stackoverflow.com/questions/28806879/animating-drawable-alpha-property –

回答

0

爲您的drawable使用xml動畫。

在res文件夾中創建文件夾anim。然後創建blink.xml文件。

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:duration="600" 
     android:repeatMode="reverse" 
     android:repeatCount="infinite"/> 
</set> 

然後如下使用它:

Animation blink; 
. 
. 
// load your animation 
animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), 
       R.anim.blink); 
. 
. 
// set animation listener 
blink.setAnimationListener(this); 


mydrawable.startAnimation(blink); 
+0

有沒有'startAnimation()'上繪製對象方法。 – BladeCoder