mirror relativelayout in android, no update - android

I want to mirror the app an use a customized relativeLayout class for the root view.
It works fine, but I habe one problem. If I refresh something in one element, that is mirrored not on the physical Location, it won't be updated. So I have to correct the coordinate, which should be updated, but I don't know how.
package de.etestdriver.everclock.gui;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
public class HUDRelativeLayout extends RelativeLayout{
public HUDRelativeLayout(Context context) {
super(context);
this.setWillNotDraw(false);
}
public HUDRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotDraw(false);
}
public HUDRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setWillNotDraw(false);
}
#Override
protected void dispatchDraw(Canvas canvas) {
canvas.scale(-1,1, getWidth()/2, getHeight()/2);
super.dispatchDraw(canvas);
canvas.restore();
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
event.setLocation( getWidth()-event.getX(), event.getY());
return super.dispatchTouchEvent(event);
}
}

Related

onTouchEvent on Path

I figured out that the width of the RectF is 0.0 so thats why it is always false. So can I set the width of the RectF width the StrokeWidth?
I have a path. And I want to handle clicks on it.
So I found out I can check that with an onTouchEvent.
So in the OnDraw() Method I draw a path and add the bounds to a rectF where I can get the left, right, top, bottom in the onTouchEvent correctly I guess.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class PathTest extends View {
Paint paint;
Path path;
RectF rectF = new RectF();
public PathTest(Context context) {
super(context);
init();
}
public PathTest(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PathTest(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.moveTo((float)getWidth()/2,getHeight()-20);
path.lineTo((float)getWidth()/2,20);
path.close();
path.computeBounds(rectF,true);
canvas.drawPath(path,paint);
}
private void init(){
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(100);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
path = new Path();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width,height/2);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(rectF.contains(event.getX(),event.getY())){
Log.d("HALLO","HI");
}else{
Log.d("HALLO","left"+rectF.left+" top"+rectF.top+" right"+rectF.right+" bottom"+rectF.bottom + " x->"+event.getX()+" y->"+event.getY());
}
}
return super.onTouchEvent(event);
}
}
It is always the else. I don't know why.
So I log the rectF Coordinates and the MotionEvent.ACTION_DOWN event.
D/HALLO: left540.0 top20.0 right540.0 bottom2240.0 x->528.0 y->490.0
D/HALLO: left540.0 top20.0 right540.0 bottom2240.0 x->1047.0 y->1021.0
D/HALLO: left540.0 top20.0 right540.0 bottom2240.0 x->1016.0 y->961.0
D/HALLO: left540.0 top20.0 right540.0 bottom2240.0 x->1059.0 y->1062.0```

Android Draw Circle with Different Text

My application draws a circle when the screen is pressed. I'm trying to put text on the circles according to how many there are the screen. So if your first tap will give you a circle with the text C0, the second will give you a circle for C1, etc.
Currently my code looks like
lPaint = new Paint();
lPaint.setColor(Color.WHITE);
lPaint.setTextAlign(Paint.Align.CENTER);
lPaint.setTextSize(40);
nCanvas.drawCircle(v.x, v.y, 55, cPaint);
nCanvas.drawText("C"+i, v.x, v.y, lPaint);
Where v.x and v.y are the coordinators where you've touched the screen, and i is the circle counter. This code starts off just fine, but after the first circle draw, it changes ALL the text for ALL the circles to the new i value. How do I get around this?
Thanks
Just create a new variable i, inside custom view. Then increment variable i inside on click and in onDraw method just draw circle, or whatever you want.For example:
package yourpackage.
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
/**
* Color view used for picking color for drawing
*/
public class ColorView extends View {
private Paint drawPaint;
private int color = ContextCompat.getColor(getContext(), android.R.color.black);
private int i;
public ColorView(Context context) {
this(context, null);
}
public ColorView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ColorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ColorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
drawPaint = new Paint();
drawPaint.setAntiAlias(true);
drawPaint.setColor(color);
drawPaint.setStyle(Paint.Style.FILL);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(0, 0, 100, 200, drawPaint);
}
public void setColor(int color) {
drawPaint.setColor(color);
this.color = color;
}
public void onClick() {
i++;
}
public int getColor() {
return color;
}
}

custom view circle truncated from right and bottom in android

I am creating a circle with custom view or canvas. The circle gets created with border as well but the problem is that the right and bottom part of circle gets truncated. I get the following output with below mentioned code,
My custom view class for creating the above mentioned class is as follows,
package cl.tk.ui.iBAPView;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import cl.tkp.R;
public class RoundedView extends View {
private Paint paint,mPaintBorder;
private int color;
float borderWidth;
private String text = null;
private boolean checked = false;
private Drawable drawable;
public RoundedView(Context context) {
super(context);
bootstrap(context, null,0,0);
}
public RoundedView(Context context, AttributeSet attrs) {
super(context, attrs);
bootstrap(context, attrs, 0,0);
}
public RoundedView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
bootstrap(context, attrs, defStyleAttr, 0);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RoundedView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
bootstrap(context, attrs, defStyleAttr, defStyleRes);
}
private void bootstrap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedView, defStyleAttr, defStyleRes);
text = a.getString(R.styleable.RoundedView_android_text);
color = a.getColor(R.styleable.RoundedView_bgColor, Color.GRAY);
drawable=a.getDrawable(R.styleable.RoundedView_drawableCenter);
checked=a.getBoolean(R.styleable.RoundedView_checked,false);
borderWidth= a.getDimension(R.styleable.RoundedView_rborderWidth,context.getResources().getDimension(R.dimen._minus2sdp));
paint = new Paint(Paint.DITHER_FLAG);
mPaintBorder = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBorder.setColor(a.getColor(R.styleable.RoundedView_borderColor,ContextCompat.getColor(context,android.R.color.transparent)));
a.recycle();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
protected void onDraw(Canvas canvas) {
//if (getBackgroundColor(this) != 0) color = getBackgroundColor(this);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
if(borderWidth>0)
{
float radius=(getWidth()-borderWidth-2)/2;
canvas.drawCircle(radius + borderWidth,radius + borderWidth,radius + borderWidth, mPaintBorder);
canvas.drawCircle(radius + borderWidth,radius + borderWidth,radius ,paint);
}else {
float radius=getWidth()/2;
canvas.drawCircle(radius, radius, radius, paint);
}
}
}
This is how i call it in my xml,
<cl.tk.ui.iBAPView.RoundedView
android:id="#+id/signup_user_rv1"
android:layout_marginRight="#dimen/_3sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="#dimen/_18sdp"
android:layout_height="#dimen/_18sdp"
app:rborderWidth="#dimen/_2sdp"
app:borderColor="#color/colorBlue"/>

Android - Dynamic Mask Shape

I am trying to implement Masking of an image for my android project.
I am trying to accomplish the same as below image :
and the output would be the same as below link:
The link below is close to what I want to achieve only that I need to create the mask at runtime and handle onTouchListener to draw the mask but how?
Masking(crop) image in frame
I can't figure out myself and I feel stuck at this problem.
Any help or tutorials that will help is highly appreciated.
Although I didn't write a complete solution it could show you right direction.
package com.example.masktest.app;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MaskDrawingView extends View {
private Path path = new Path();
private Paint pathPaint = new Paint();
public MaskDrawingView(Context context) {
super(context);
init();
}
public MaskDrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MaskDrawingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
pathPaint.setStyle(Paint.Style.STROKE);
pathPaint.setAlpha(150);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, pathPaint);
invalidate();
}
#Override
public boolean onTouchEvent(#NonNull MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.lineTo(event.getX(), event.getY());
break;
}
return super.onTouchEvent(event);
}
public Bitmap finishDrawingAndGetMask() {
pathPaint.setStyle(Paint.Style.FILL);
path.close();
setDrawingCacheEnabled(true);
Bitmap result = Bitmap.createBitmap(getDrawingCache());
setDrawingCacheEnabled(false);
path.reset();
return result;
}
}

[Android]canvas.drawText is very slow while changing text size continuously

I'm trying to scale the text continuously by changing text size. And I implement my own view to drawText on canvas every time the text size changed. But the procedure of invalidate and redraw canvas became very slow which lead to some animation became slow too.
The specific os version is 4.0.3(The application works well on the same kind device with os version 4.1.1)
The hardware acceleration is on.(works well when it's off)
I wrote a simple project to reproduce this issue.
The custom view:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyTextView extends View {
private Paint mPaint;
private float mTextSize = 80f;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attr) {
super(context, attr);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(mTextSize);
}
#Override
public void onDraw(Canvas canvas) {
canvas.save();
mPaint.setTextSize(mTextSize);
canvas.drawText("Test test test", 200, 200, mPaint);
canvas.restore();
mTextSize += 1f;
}
}
The Activity:
import android.os.Bundle;
import android.view.MotionEvent;
import android.app.Activity;
public class MainActivity extends Activity {
private MyTextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (MyTextView)findViewById(R.id.text);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mTextView.invalidate();
return true;
}
}
Finally I used StaticLayout.draw(Canvas) instead call Canvas.drawText

Categories

Resources