Untitled
unknown
plain_text
8 months ago
4.7 kB
5
Indexable
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Tạo ImageView để hiển thị ảnh
ImageView imageView = findViewById(R.id.imageView);
// Kích thước của Bitmap
int width = 600, height = 400;
// Tạo một Bitmap có nền trong suốt
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Tạo Canvas để vẽ lên Bitmap
Canvas canvas = new Canvas(bitmap);
// Tạo Paint để vẽ
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// Mảng màu
int color1 = Color.RED;
int color2 = Color.BLUE;
int color3 = Color.RED; // Trùng với color1
// Vẽ 3 hình chữ nhật với 3 màu
paint.setColor(color1);
canvas.drawRect(0, 0, width / 3, height, paint); // Hình chữ nhật 1
paint.setColor(color2);
canvas.drawRect(width / 3, 0, 2 * width / 3, height, paint); // Hình chữ nhật 2
paint.setColor(color3);
canvas.drawRect(2 * width / 3, 0, width, height, paint); // Hình chữ nhật 3 (giống color1)
// Đặt Bitmap vào ImageView
imageView.setImageBitmap(bitmap);
}
}
}
}Editor is loading...
Leave a Comment