Android30
Canvas.translate方法
public voidtranslate(float dx, float dy) {
if (dx == 0.0f && dy == 0.0f) return;
nTranslate(mNativeCanvasWrapper, dx, dy);
}
演示
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class MyView extends View {
public MyView(Context context) {
super(context);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GRAY);
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
canvas.drawRect(0, 0, 300, 300, mPaint);
canvas.translate(200, 200);
mPaint.setColor(Color.BLUE);
canvas.drawRect(0, 0, 300, 300, mPaint);
canvas.translate(200, 200);
mPaint.setColor(Color.RED);
canvas.drawRect(0, 0, 300, 300, mPaint);
}
}
代码
示例代码:地址
参考文档
Android Developers 中描述的 Canvas.translate方法,参考文档:地址
|