コンキスタドール:
私はそれをただ水平プログレスバーのような左から右に色を充填することにより、押圧しながら、ボタンの背景を変更したいです。
これは、プレスアンドホールドを検出するためのコードです:
lateinit var buttonView: View
val cdt = object : CountDownTimer(1000, 100) {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
//do something after being press by 1 sec.
}
}
val onTouchListener = View.OnTouchListener { v, event ->
buttonView = v
when (event.action) {
MotionEvent.ACTION_DOWN -> {
(v.background as AnimationDrawable).start()
cdt.start()
}
MotionEvent.ACTION_UP -> {
(v.background as AnimationDrawable).stop()
cdt.cancel()
}
}
false
}
これは私がボタンに使用する背景です。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
...items with drawables which colors are in different position. Frame by frame....
</animation-list>
これは動作しますが、それは滑らかではありません。これを効率的に行う方法はありますか?ちょうどこのような?https://codepen.io/kay8/pen/azKbjN
(これはJavaでお答えしても大丈夫です)
Cheticampの:
描画可能なために、私は行くだろうClipDrawable
のトップ層としてLayerDrawable
。これは、視覚的にプログレスバーのように動作します。
次の描画可能では、クリップ描画可能なだけ赤長方形である底部層に重なります。
button_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_red_light" />
</shape>
</item>
<item android:id="@+id/clip_drawable">
<clip
android:clipOrientation="horizontal"
android:gravity="start">
<shape
android:shape="rectangle">
<solid android:color="@android:color/black" />
</shape>
</clip>
</item>
</layer-list>
クリップ描画可能の黒は0(図示せず)から10,000クリップ描画可能な増加のレベルとして赤い長方形を凌ぐであろう(赤い矩形をカバー100%)。
TimeAnimator
クリップ描画可能のレベルをアニメーション化するために使用することができます。
このクラスは、システム内の他のすべてのアニメーターと同期しているリスナーに、単純なコールバックメカニズムを提供します。
ここでは簡単のデモです。あなたは、変更することができonTimeUpdate()
ますが、アニメーションの継続時間を設定したい場合は、合計経過時間に敏感であることが。
MainActivity.java
public class MainActivity extends AppCompatActivity implements TimeAnimator.TimeListener {
private TimeAnimator mAnimator;
private int mCurrentLevel = 0;
private ClipDrawable mClipDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a handle on the ClipDrawable that we will animate.
LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.button).getBackground();
mClipDrawable = (ClipDrawable) layerDrawable.findDrawableByLayerId(R.id.clip_drawable);
// Set up TimeAnimator to fire off on button click.
mAnimator = new TimeAnimator();
mAnimator.setTimeListener(this);
}
@Override
public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) {
mClipDrawable.setLevel(mCurrentLevel);
if (mCurrentLevel >= MAX_LEVEL) {
mAnimator.cancel();
} else {
mCurrentLevel = Math.min(MAX_LEVEL, mCurrentLevel + LEVEL_INCREMENT);
}
}
public void animateButton(View view) {
if (!mAnimator.isRunning()) {
mCurrentLevel = 0;
mAnimator.start();
}
}
private static final int LEVEL_INCREMENT = 400;
private static final int MAX_LEVEL = 10000;
}
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:background="@drawable/button_background"
android:text="Button"
android:onClick="animateButton"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>