1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.androidx.drawable; 18 19 import android.graphics.Canvas; 20 import android.graphics.ColorFilter; 21 import android.graphics.Paint; 22 import android.graphics.PixelFormat; 23 import android.graphics.drawable.Drawable; 24 25 import org.jspecify.annotations.NonNull; 26 27 /** 28 * Simple custom drawable. 29 */ 30 public class MyDrawable extends Drawable { 31 private final Paint mPaint; 32 MyDrawable()33 public MyDrawable() { 34 mPaint = new Paint(); 35 mPaint.setARGB(255, 255, 0, 0); 36 mPaint.setAntiAlias(true); 37 } 38 39 @Override draw(@onNull Canvas canvas)40 public void draw(@NonNull Canvas canvas) { 41 // Get the drawable's bounds 42 int width = getBounds().width(); 43 int height = getBounds().height(); 44 float radius = Math.min(width, height) / 2; 45 46 // Draw a red circle in the center 47 canvas.drawCircle(width / 2, height / 2, radius, mPaint); 48 } 49 50 @Override setAlpha(int alpha)51 public void setAlpha(int alpha) { 52 // This method is required 53 } 54 55 @Override setColorFilter(ColorFilter colorFilter)56 public void setColorFilter(ColorFilter colorFilter) { 57 // This method is required 58 } 59 60 @Override getOpacity()61 public int getOpacity() { 62 return PixelFormat.OPAQUE; 63 } 64 } 65