カスタムビューにドローアブルがたくさんあります。ユーザーが 1 つまたは複数のドローアブルを押すと、色が変わるようにしたいと考えています。現在、各ドローアブルはStateListDrawable
2 つの状態 (state_pressed
押されていない) のみです。ドローアブルを押すたびにsetState
trueが返されるので、実際に変更されていると想定していますが、ドローアブルの画像が変化することはありません。invalidateDrawable
何もしていませんか?私は何を間違っていますか?customView.invalidate()
呼び出しを必要とせずに毎回すべてを再描画することなく、押されたときに1つのドローアブルを再描画するにはどうすればよいですか? 私はもともとそれを行っていましたが、アプリの実行が非常に遅い/非効率的であることがわかりました。ありがとう!
流れ:
Custom View (contains set of our custom class - TouchKey)
- Custom class TouchKey containing drawable and info
- Upon press or release, custom class finds which drawable to change
クラス内のボタン タッチのコードを次に示しますTouchKey
(MyTouch は、Android デバイス上のすべてのタッチを追跡するカスタム クラスです)。
public void pressed(MyTouch touch) {
boolean successfulStateChange = this.drawable.setState(new int[]{android.
R.attr.state_pressed});
this.customView.invalidateDrawable(drawable);
}
public void released(MyTouch touch) {
boolean successfulStateChange = this.drawable.setState(new int[]{-android.
R.attr.state_pressed});
this.customView.invalidateDrawable(drawable);
}
StateListDrawable
カスタム ビューでの描画方法:
public class CustomView extends View {
private TreeMap<Integer, TouchKey> keymap;
/* Initialization Code Stuff Here - call drawKey */
// StateListDrawable Creation
private StateListDrawable drawKey(Canvas canvas, int bounds_l,
int bounds_t, int bounds_r, int bounds_b)
throws Resources.NotFoundException, XmlPullParserException, IOException {
StateListDrawable key = new StateListDrawable();
key.addState(new int[]{android.R.attr.state_pressed},
ContextCompat.getDrawable(mContext, R.drawable.key_pressed));
key.addState(new int[]{-android.R.attr.state_pressed},
ContextCompat.getDrawable(mContext, R.drawable.key_released));
key.setBounds(bound_l, bounds_t, bounds_r, bounds_b);
key.draw(canvas);
return key;
}
}