多线程与异步
new Thread(() -> {
// 子线程任务
}).start();
// Handler用法
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
// 主线程更新UI
});
性能优化技巧
- • 避免内存泄漏(使用弱引用、及时释放资源)
- • 布局优化(减少嵌套、使用ConstraintLayout)
- • 图片压缩与缓存
动画与自定义View
// 属性动画
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
animator.setDuration(1000);
animator.start();
// 自定义View
public class MyView extends View {
public MyView(Context context) { super(context); }
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100, 100, 50, new Paint());
}
}