This is my little app. It looks perfect in my device but when I use it in another device with another size screen the rectangle does not fit the screen to the edge.I try to convert pix to dp but it does not work great.
public class MyView extends View {
private static final String TAG = "MyActivity";
Paint paint;
private Rect rectangle;
Paint paint2;
private Rect rectangle2;
Paint paint3;
private Rect rectangle3;
Paint paint4;
private Rect rectangle4;
Paint paint5;
private Rect rectangle5;
Paint paint6;
private Rect rectangle6;
Paint paint7;
private Rect rectangle7;
Paint paint8;
private Rect rectangle8;
Paint paint9;
private Rect rectangle9;
Paint paint10;
private Rect rectangle10;
Paint paint11;
private Rect rectangle11;
Paint paint12;
private Rect rectangle12;
Paint paint13;
private Rect rectangle13;
Paint paint14;
private Rect rectangle14;
Paint paint15;
private Rect rectangle15;
Paint paint16;
private Rect rectangle16;
Paint colorPicker1;
private Rect rectangleRed;
Paint colorPicker2;
private Rect rectangleYellow;
Paint colorPicker3;
private Rect rectangleBlue;
Paint colorPicker4;
private Rect rectangleWhite;
int xStored;
int yStored;
int colorStore1;
public MyView(Context context) {
super(context);
init();
}
private void init(){
Log.i(TAG, "hola "+ screen);
paint = new Paint();
paint.setColor(Color.WHITE);
rectangle = new Rect(0, 0, 100, 100);
paint2 = new Paint();
paint2.setColor(Color.WHITE);
rectangle2 = new Rect(110, 0, 250, 100);
paint3 = new Paint();
paint3.setColor(Color.WHITE);
rectangle3 = new Rect(260, 0, 340, 100);
paint4 = new Paint();
paint4.setColor(Color.WHITE);
rectangle4 = new Rect(350, 0, 480, 100);
paint5 = new Paint();
paint5.setColor(Color.WHITE);
rectangle5 = new Rect(0, 110, 100, 260);
paint6 = new Paint();
paint6.setColor(Color.WHITE);
rectangle6 = new Rect(110, 110, 250, 260);
paint7 = new Paint();
paint7.setColor(Color.WHITE);
rectangle7 = new Rect(260, 110, 340, 260);
paint8 = new Paint();
paint8.setColor(Color.WHITE);
rectangle8 = new Rect(350, 110, 480, 260);
paint9 = new Paint();
paint9.setColor(Color.WHITE);
rectangle9 = new Rect(0, 270, 100, 340);
paint10 = new Paint();
paint10.setColor(Color.WHITE);
rectangle10 = new Rect(110, 270, 250, 340);
paint11 = new Paint();
paint11.setColor(Color.WHITE);
rectangle11 = new Rect(260, 270, 340, 340);
paint12 = new Paint();
paint12.setColor(Color.WHITE);
rectangle12 = new Rect(350, 270, 480, 340);
paint13 = new Paint();
paint13.setColor(Color.WHITE);
rectangle13 = new Rect(0, 350, 100, 440);
paint14 = new Paint();
paint14.setColor(Color.WHITE);
rectangle14 = new Rect(110, 350, 250, 440);
paint15 = new Paint();
paint15.setColor(Color.WHITE);
rectangle15 = new Rect(260, 350, 340, 440);
paint16 = new Paint();
paint16.setColor(Color.WHITE);
rectangle16 = new Rect(350, 350, 480, 440);
colorPicker1 = new Paint();
colorPicker1.setColor(Color.RED);
rectangleRed= new Rect(0, 580 , 120+ screen, 680);
colorPicker2 = new Paint();
colorPicker2.setColor(Color.YELLOW);
rectangleYellow = new Rect(120+ screen, 580, 240+ screen2, 680);
colorPicker3 = new Paint();
colorPicker3.setColor(Color.BLUE);
rectangleBlue = new Rect(240+ screen3, 580, 360,680 );
colorPicker4 = new Paint();
colorPicker4.setColor(Color.WHITE);
rectangleWhite = new Rect(360+ screen, 580, 480+ screen, 680);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(BLACK);
canvas.drawRect(rectangle, paint);
canvas.drawRect(rectangle2, paint2);
canvas.drawRect(rectangle3, paint3);
canvas.drawRect(rectangle4, paint4);
canvas.drawRect(rectangle5, paint5);
canvas.drawRect(rectangle6, paint6);
canvas.drawRect(rectangle7, paint7);
canvas.drawRect(rectangle8, paint8);
canvas.drawRect(rectangle9, paint9);
canvas.drawRect(rectangle10, paint10);
canvas.drawRect(rectangle11, paint11);
canvas.drawRect(rectangle12, paint12);
canvas.drawRect(rectangle13, paint13);
canvas.drawRect(rectangle14, paint14);
canvas.drawRect(rectangle15, paint15);
canvas.drawRect(rectangle16, paint16);
canvas.drawRect(rectangleRed, colorPicker1);
canvas.drawRect(rectangleYellow,colorPicker2 );
canvas.drawRect(rectangleBlue,colorPicker3 );
//canvas.drawRect(rectangleWhite,colorPicker4 );
}
#Override
public boolean onTouchEvent( MotionEvent event) {
super.onTouchEvent(event);
int x = (int)event.getX();
int y = (int)event.getY();
xStored = x; yStored=y;
if (event.getAction()==MotionEvent.ACTION_UP){
}else if(event.getAction()==MotionEvent.ACTION_DOWN){
if(rectangleRed.contains(x,y)){
colorStore1 = colorPicker1.getColor();
}
if(rectangleYellow.contains(x,y)){
colorStore1 = colorPicker2.getColor();
}
if (rectangleBlue.contains(x,y)){
colorStore1 = colorPicker3.getColor();
}
if (rectangleWhite.contains(x,y)){
colorStore1 = colorPicker4.getColor();
}
if(rectangle.contains(x,y)){
paint.setColor(colorStore1);
}
if(rectangle2.contains(x,y)){
paint2.setColor(colorStore1);
}
if(rectangle3.contains(x,y)){
paint3.setColor(colorStore1);
}
if(rectangle4.contains(x,y)){
paint4.setColor(colorStore1);
}
if(rectangle5.contains(x,y)){
paint5.setColor(colorStore1);
}
if(rectangle6.contains(x,y)){
paint6.setColor(colorStore1);
}
if(rectangle7.contains(x,y)){
paint7.setColor(colorStore1);
}
if(rectangle8.contains(x,y)){
paint8.setColor(colorStore1);
}
if(rectangle9.contains(x,y)){
paint9.setColor(colorStore1);
}
if(rectangle10.contains(x,y)){
paint10.setColor(colorStore1);
}
if(rectangle11.contains(x,y)){
paint11.setColor(colorStore1);
}
if(rectangle12.contains(x,y)){
paint12.setColor(colorStore1);
}
if(rectangle13.contains(x,y)){
paint13.setColor(colorStore1);
}
if(rectangle14.contains(x,y)){
paint14.setColor(colorStore1);
}
if(rectangle15.contains(x,y)){
paint15.setColor(colorStore1);
}
if(rectangle16.contains(x,y)){
paint16.setColor(colorStore1);
}
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
}
this.postInvalidate();
return true;
}
}
Use the getWidth() and getHeight() methods of View to get width and height at runtime, matching the actual screen.
Related
How to make that circles rotate in different directions?I have to make a Solar system animation.In the center is Sun and the planet rotate around sun with different direction and speed.Problem is that all circles are turning in clockwise directions and the degrees in rotate() method is negative for blue and gray circle...Thanks in advance
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Timer timer;
Canvas canvas;
Paint paint, paint1, paint2, paint3, paint4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = Bitmap.createBitmap(1000, 1500, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.parseColor("#EBEBEB"));
canvas = new Canvas();
canvas.setBitmap(bitmap);
paint = new Paint();
paint1 = new Paint();
paint2 = new Paint();
paint3 = new Paint();
paint4 = new Paint();
paint.setColor(Color.YELLOW);
paint1.setColor(Color.RED);
paint2.setColor(Color.BLUE);
paint3.setColor(Color.GREEN);
paint4.setColor(Color.GRAY);
canvas.drawCircle(500f, 750f, 130, paint);
canvas.drawCircle(400f, 560f, 51, paint1);
canvas.drawCircle(750f, 920f, 48, paint2);
canvas.drawCircle(300f, 1100f, 45, paint3);
canvas.drawCircle(320f, 300f, 42, paint4);
imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(
new Runnable() {
#Override
public void run() {
animationFrame();
}
}
);
}
}, 3000, 80);
}
private void animationFrame() {
canvas.drawColor(Color.parseColor("#EBEBEB"));
paint.setColor(Color.YELLOW);
canvas.drawCircle(500f, 750f, 130f, paint);
if (paint1 != null | paint3 != null) {
paint1.setColor(Color.RED);
canvas.drawCircle(400f, 560f, 51f, paint1);
canvas.rotate(10f, 500f, 750f);
paint3.setColor(Color.GREEN);
canvas.drawCircle(350f, 1050f, 45, paint3);
canvas.rotate(30f, 500f, 750f);
}
if (paint2 != null | paint4 != null) {
paint2.setColor(Color.BLUE);
canvas.drawCircle(720f, 950f, 48, paint2);
canvas.rotate(-10f, 500f, 750f);
paint4.setColor(Color.GRAY);
canvas.drawCircle(290f, 330f, 42, paint4);
canvas.rotate(-25f, 500f, 750f);
}
imageView.invalidate();
}
I'm trying to create a custom shape using the Path object in Android and I'm running into a weird problem.
What I'm trying to achieve is depicted in the picture below
Here is the code I'm using to draw and fill the shape:
public class BallView extends RelativeLayout {
....
protected void onDraw(Canvas canvas) {
...
PaintArc(canvas);
}
private void PaintArc(Canvas canvas) {
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.FILL_AND_STROKE);
p.setStrokeWidth(2);
p.setColor(Color.RED);
RectF oval = new RectF(20, 20, getWidth() - 20, getHeight() - 20);
RectF oval2 = new RectF(0, 0, getWidth(), getHeight());
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.addArc(oval, 0, 180);
path.addArc(oval2, 0, 180);
float y=20+oval.height()/2;
float x=20;
path.moveTo(x,y);
path.lineTo(x - 20, y);
x=oval.width()+20;
path.moveTo(x,y);
path.lineTo(x+20,y);
path.close();
canvas.drawPath(path, p);
}
}
The actual result that I'm getting looks like this:
The resulting shape without Filling looks like this:
Can you tell me what I'm doing wrong ?
try this:
class MyView extends View {
private Path mPath;
private Paint mPaint;
private RectF mOval;
public MyView(Context context) {
super(context);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(0xffff0000);
mOval = new RectF();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int dx = w / 4;
mOval.set(0, 0, w, w);
mPath.reset();
mPath.moveTo(0, w / 2f);
mPath.arcTo(mOval, 180, 180);
mPath.rLineTo(-dx, 0);
mOval.inset(dx, dx);
mPath.addArc(mOval, 0, -180);
mPath.rLineTo(-dx, 0);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xffffffff);
mPaint.setStyle(Style.FILL);
canvas.drawPath(mPath, mPaint);
canvas.translate(0, getWidth() / 2);
mPaint.setStyle(Style.STROKE);
canvas.drawPath(mPath, mPaint);
}
}
- I have just edited my answer
and i add below code
RectF oval3 = new RectF(10, 20, getWidth() - 10, getHeight() - 10);
/**
*Now check this code output seem like your thought
*/
private void PaintArc(Canvas canvas) {
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(2);
p.setColor(Color.RED);
Paint p1 = new Paint(Paint.ANTI_ALIAS_FLAG);
p1.setAntiAlias(true);
p1.setStyle(Paint.Style.STROKE);
p1.setStrokeWidth(10);
p1.setColor(Color.RED);
RectF oval = new RectF(20, 20, getWidth() - 20, getHeight() - 20);
RectF oval2 = new RectF(0, 0, getWidth(), getHeight());
RectF oval3 = new RectF(10, 20, getWidth() - 10, getHeight() - 10);
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.addArc(oval, 0, 180);
path.addArc(oval2, 0, 180);
path.addArc(oval3, 0, 180);
float y = 20 + oval.height() / 2;
float x = 20;
path.moveTo(x, y);
path.lineTo(x - 20, y);
x = oval.width() + 20;
path.moveTo(x, y);
path.lineTo(x + 20, y);
path.close();
canvas.drawArc(oval3, 0, 180, false, p1);
canvas.drawPath(path, p);
}
I'm trying to draw two circles like this:
This is how I'm trying to do it:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(bmp);
RectF rect = new RectF(0,0,width,width);
Paint paint = new Paint();
drawCircles(paint, c, width, height, width);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
img.setScaleType(ScaleType.FIT_CENTER);
And here is my drawCircles() method:
private void drawCircles(Paint paint, Canvas c, int width, int height, int radius) {
paint.setARGB(255, 255 , 10, 21);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.BUTT);
paint.setStyle(Paint.Style.STROKE);
if(width < height && radius == 0){
radius = width/2;
height = width;
} else if (radius == 0){
radius = height/2;
width = height;
}
Paint paint2 = new Paint();
paint2.setARGB(255, 255 , 10, 21);
paint2.setStrokeWidth(10);
paint2.setAntiAlias(true);
paint2.setStrokeCap(Paint.Cap.BUTT);
paint2.setStyle(Paint.Style.STROKE);
c.drawCircle(width/2, height/2, radius-10, paint);
c.drawCircle(width/2, height/2, 50, paint2);
}
I don't know why but I get only one circle, the small one (the one drawn with paint2).
What can be the reason?
Try this code.Hope it may helps :)
public class SimpleCircleActivity extends Activity
{
private CircleDemoView circledemoView ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
circledemoView =new CircleDemoView(this);
setContentView(circledemoView);
}
private class CircleDemoView extends View
{
public CircleDemoView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);
p.setPathEffect(dashPath);
p.setStyle(Style.STROKE);
for (int i = 0; i < 2; i ++) {
canvas.drawCircle(200, 200, 50+(i*40), p);
}
invalidate();
}
}
}
I need to round the corners of a Bitmap. After that, I need to add a border for it. I have done the source below to round the corners but I don't know how to draw a border in the Bitmap using Canvas. Is there any way to do it? Thanks
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
I have added the solution:
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
Paint paintStroke = new Paint();
paintStroke.setStrokeWidth(2);
paintStroke.setStyle(Paint.Style.STROKE);
paintStroke.setColor(Color.RED);
paintStroke.setAntiAlias(true);
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
canvas.drawRoundRect(rectF, round, round, paintStroke);
return output;
try this Method and pass bitmap it will return bitmap with border with angle :
Bitmap rotateAndFrame(Bitmap bitmap,float angle) {
// final boolean positive = sRandom.nextFloat() >= 0.5f;
// final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() *
// ROTATION_ANGLE_EXTRA) * (positive ? 1.0f : -1.0f);
final double radAngle = Math.toRadians(angle);
final int bitmapWidth = bitmap.getWidth();
final int bitmapHeight = bitmap.getHeight();
final double cosAngle = Math.abs(Math.cos(radAngle));
final double sinAngle = Math.abs(Math.sin(radAngle));
final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);
final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);
final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);
final float x = (width - bitmapWidth) / 2.0f;
final float y = (height - bitmapHeight) / 2.0f;
final Bitmap decored = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(decored);
canvas.rotate(angle, width / 2.0f, height / 2.0f);
canvas.drawBitmap(bitmap, x, y, sPaint);
canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);
canvas.drawText(String.valueOf(k_value),(one_piecewidth*0.84f),
(one_pieceheight*0.35f),sNumPaint);
k++;
return decored;
}
hope it helps...
edited:
private static final float PHOTO_BORDER_WIDTH2 = 3.0f;
private static final float PHOTO_BORDER_WIDTH = 1.0f;
private static final int PHOTO_BORDER_COLOR2=0x00000000;
private static final int PHOTO_BORDER_COLOR = 0xffffffff;
private static final Paint sPaint =
new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
private static final Paint sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private static final Paint sNumPaint = new Paint(Paint.LINEAR_TEXT_FLAG);
static {
sNumPaint.setColor(Color.BLACK);
sNumPaint.setTextSize(25);
sNumPaint.setTextAlign(Align.RIGHT);
}
static {
sStrokePaint.setStrokeWidth(PHOTO_BORDER_WIDTH);
sStrokePaint.setStyle(Paint.Style.STROKE);
// sStrokePaint.measureText("hello");
sStrokePaint.setColor(PHOTO_BORDER_COLOR);
}
I'm trying to add shadow effect do an image. I use a mask to draw the image (I need a specific shape for my image). Can you please tell me how to add shadow effect to my image? I've tried something like paint.setShadowLayer(10, 10, 10, Color.RED) but it didn't worked. Here is the source code:
#Override
public void draw(Canvas canvas) {
Rect rect = new Rect(0, 0, getWidth() - 1, getHeight() - 1);
NinePatchDrawable mask = (NinePatchDrawable) getContext().getResources().getDrawable(maskResId);
mask.setBounds(rect);
Bitmap content = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
Canvas contentCanvas = new Canvas(content);
super.draw(contentCanvas);
Paint paint = new Paint();
paint.setXfermode(new AvoidXfermode(Color.BLACK, 255, AvoidXfermode.Mode.TARGET));
mask.draw(canvas);
canvas.drawBitmap(content, null, rect, paint);
}
my solution:
class ShadowImage
public class ShadowImage extends BitmapDrawable {
Bitmap bm;
static float shadowRadius = 4f;
static PointF shadowDirection = new PointF(2f, 2f);
int fillColor = 0;
#Override
public void draw(Canvas canvas) {
Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
Log.i("TEST", rect.toString());
setBounds(rect);
Paint mShadow = new Paint();
mShadow.setAntiAlias(true);
mShadow.setShadowLayer(shadowRadius, shadowDirection.x, shadowDirection.y, Color.BLACK);
canvas.drawRect(rect, mShadow);
if(fillColor != 0) {
Paint mFill = new Paint();
mFill.setColor(fillColor);
canvas.drawRect(rect, mFill);
}
canvas.drawBitmap(bm, 0.0f, 0.0f, null);
}
public ShadowImage(Resources res, Bitmap bitmap) {
super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false));
this.bm = bitmap;
}
public ShadowImage(Resources res, Bitmap bitmap, int fillColor) {
super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false));
this.bm = bitmap;
this.fillColor = fillColor;
}
}
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout root = (LinearLayout) findViewById(R.id.root_layout);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ShadowImage image = new ShadowImage(getResources(), bmp);
ShadowImage image2 = new ShadowImage(getResources(), bmp, Color.WHITE);
ImageView iv_normal = new ImageView(getApplicationContext());
iv_normal.setPadding(10, 10, 10, 10);
iv_normal.setImageBitmap(bmp);
ImageView iv_shadow = new ImageView(getApplicationContext());
iv_shadow.setPadding(10, 10, 10, 10);
iv_shadow.setImageDrawable(image);
ImageView iv_fill = new ImageView(getApplicationContext());
iv_fill.setPadding(10, 10, 10, 10);
iv_fill.setImageDrawable(image2);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
root.addView(iv_normal, params);
root.addView(iv_shadow, params);
root.addView(iv_fill, params);
root.setGravity(Gravity.CENTER);
}
Image: