I have created an activity that uses a canvas on which I can draw.
Within the activity, I need to retrieve data from a table that I have created elsewhere.
In other activities, I have used the database handler below and it works fine.
In this it does not, I think because I have extended imageview (required for the canvas) rather than extending Activity.
Thank you in advance for any help....
Here is my code:
public class examplecanvas extends ImageView {
float xPos, yPos;
private PointF point;
private Paint paint = new Paint();
public examplecanvas(Context context) {
super(context);
}
public examplecanvas(Context context, AttributeSet attrs) {
super(context, attrs);
}
public examplecanvas(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onTouchEvent(#NonNull MotionEvent event) {
float x = event.getX();
float y = event.getY();
}
return true;
}
#Override
protected void onDraw(#NonNull Canvas canvas) {
super.onDraw(canvas);
if (point != null) {
canvas.drawCircle(point.x, point.y, 100, paint);
canvas.save();
}
}
public void getmydata() {
String parenta = "parent"
myDBhandler1 dbHandler;
dbHandler = new myDBhandler1(examplecanvas.this, null, null, 1); <<<<HERE IS THE ERROR
}
}
...and this is the error I get:
Your database handler requires a context, so you could pass
new myDbHandler1(getContext(), ...);
"examplecanvas.this" does not have context like you want to provide to the DBHandler constructor. You should pass Context in your getmydata() function as a parameter (Then use YourActivity.this or getActivity() when you call this method, depends on Activity or Fragemnt). Or simply use the Context received in your extended ImageView constructors.
Related
I am new to android & java. This might be very simple but I'm stuck... I am creating an app where by clicking on an "add" button a square is added in some default position on a canvas. You can then move and resize the square. Once you finish moving/ reshaping, my intention is for the square to maintain its new size/ position and for the user to be able to click on the add button where a new square will appear in the original default position. You can then again move / reshape the second square. And eventually add as many squares as you like (each unique).
I have managed to add the first square and i am also able to move/ reshape. I also created an array list to store the squares. My issue is that as soon as i add the second square, the shape and position of the first square are reset to the default position and size. And if i move the second square, so does the first one. So effectively its as if i end up with a stacked set of squares that all move and reshape at the same time.
The question is, how do i convert the variable information of position/ size to a constant before i add the square to the array?
I have the following in my main activity
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(addTimer != null) {
addTimer.cancel();
}
if (addclickcount>0) {
addBoxtoList();
}
addBox();
addclickcount++;
}
});
public void addBox(){ mCustomView.drawRoundRect();}
public void addBoxtoList(){mCustomView.addBoxtoList();}
Then in CustomView
public class CustomView extends View {
private ArrayList<Box> boxesArray = new ArrayList<>();
private RectF mRectSquare;
private Paint mPaintSquare;
private static RectF aRectSquare;
private static Paint aPaintSquare;
private int mSquareLeft, mSquareTop, dx, dy;
int colorSelected;
public CustomView(Context context) {
super(context);
init(null);
}
public CustomView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
public CustomView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init (#Nullable AttributeSet set){
mRectSquare = new RectF();
mPaintSquare = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintSquare.setColor(Color.BLUE);
mPaintSquare.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintSquare.setAlpha(85);
}
public void drawRoundRect(){
if (General.addBoxCheck == true) {
dx = 200;
dy = 200;
mSquareLeft = scrwd - dx - 50;
mSquareTop = 50;
General.addBoxCheck = false;
}
mRectSquare.left = mSquareLeft;
mRectSquare.top = mSquareTop;
mRectSquare.right = mRectSquare.left + dx;
mRectSquare.bottom = mRectSquare.top + dy;
postInvalidate();
}
public void addBoxtoList(){
boxid = boxesArray.size() + 1;
aRectSquare = mRectSquare;
aPaintSquare = mPaintSquare;
Box boxa = new Box(boxid, aRectSquare, aPaintSquare);
boxesArray.add(boxa);
//postInvalidate();
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawRoundRect(mRectSquare, 15, 15, mPaintSquare);
if(boxesArray != null) {
RectF rectF = new RectF();
Paint paint = new Paint();
for (int i = 0; i < boxesArray.size(); i++) {
paint = boxesArray.get(i).getPaint();
rectF = boxesArray.get(i).getRect();
canvas.drawRoundRect(rectF, 15, 15, paint);
}
}
super.onDraw(canvas);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
boolean value = super.onTouchEvent(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
return true;
}
case MotionEvent.ACTION_MOVE:{
int x = (int) event.getX();
int y = (int) event.getY();
mSquareLeft = x;
mSquareTop = y;
postInvalidate();
return value;
}
}
return value;
}
Thanks
The question seems quite vague, if the values are changing then it means that you changed the values. If you don’t change the values of the array then essentially it should be a "constant". My assumption is that you are overwriting the array and therefore the value is changing. It would help to see your code.
Check the index of the stored square shapes and verify that you're not overwriting on any of the previous indexes.
I have 12 lines I've created using the following class
public class LineView extends View {
private Paint paint = new Paint();
private PointF pointA,pointB;
// private void init() {
// paint.setColor(Color.BLACK);
// }
public LineView(Context context) {
super(context);
// init();
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
// init();
}
public LineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// init();
}
#SuppressLint("ResourceAsColor")
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int color = R.color.GradientStart;
paint.setColor(color);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
//canvas.drawLine(x1, y1, x2, y2, paint);
canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint);
}
public void setPointA(PointF point){
pointA=point;
}
public void setPointB(PointF point){
pointB=point;
}
public void draw(){
invalidate();
requestLayout();
}
}
Instead of lines I what lines with arrows. The line with arrow will be drawn between buttons.
How can I add arrows to one end of my lines?
It would like like this when complete.
thanks
JN
I found two way for achieve your requirement.
1) To use nine patch image. I have tried to make nine patch image for you please use it
2) You can use vector image for it.
Tell me still you not getting solution.
I need to set white color to the region that the user touches on an ImageView. If I setOnTouchListener to the ImageView and get the x and y positions of the touch, how can I change the appropriate pixel values in the ImageView? or is there a better solution?
I think the easiest solution is to extend ImageView.
Here is a brief example that draws a black circle around touched area:
class TouchableImageView extends ImageView {
private float x, y;
private Paint paint;
public TouchableImageView(Context context) {
super(context);
init();
}
public TouchableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TouchableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TouchableImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
void init() {
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = event.getX();
y = event.getY();
invalidate();
}
return super.onTouchEvent(event);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Just for example - draw a circle around touch area:
if(x!=0 || y!=0)
canvas.drawCircle(x, y, 25, paint);
}
}
Edit :
If you want to save the result as a bitmap - you need several more steps as described here and here.
In short you should follow these steps :
Create a new Bitmap of the required size
Create new canvas for the created Bitmap - new Canvas(bitmap)
Re-draw all you need upon this canvas
Save bitmap as a graphics file
Hy, I'm trying to do an animation of a circle drawn on canvas. I can do that pretty easily with ObjectAnimator, however I'd like to start the animation when the view finishes loading or finishes drawing. If I start the animation in init(), the animation property will be "ahead" of the actual drawing, so I need to start it on a callback when the whole view is properly set up. I could do that onMeasure() or onSizeChanged() but those two get called too many times and if i have nested layouts it doesn't work properly. If I use startDelay() it works but I don't think that is an accurate procedure.
Here is a basic custom view class with animation property that changes the radius of a circle.
public class CustomView extends View {
private static final String TAG = CustomView.class.toString();
public CustomView(final Context context) {
super(context);
init(context);
}
public CustomView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(final Context context) {
// OUTER CIRCLE PAINT
mPaint = new Paint();
// Adds anti-aliasing to drawed elements
mPaint.setAntiAlias(true);
mPaint.setFilterBitmap(true);
mPaint.setStrokeWidth(1);
mPaint.setStrokeCap(Paint.Cap.SQUARE);
mPaint.setStyle(Paint.Style.FILL);
final int animationTime = getResources().getInteger(ANIMATION_TIME_ID);
progressAnimator = ObjectAnimator.ofFloat(this, "animProgress", 0f, 0f);
progressAnimator.setDuration(animationTime);
Log.d(TAG, "Init ended");
//startAnimationCircle(50f);
}
#Override
public void onDraw(final Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(this.getWidth()/2, this.getHeight()/2, animProgress, mPaint);
}
#Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
//startAnimationCircle(50f);
}
/**
* onMeasure() is called automatically right after a call to measure()
*/
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//startAnimationCircle(50f);
}
private Paint mPaint;
private static final int ANIMATION_TIME_ID = android.R.integer.config_mediumAnimTime;
private float animProgress;
private ObjectAnimator progressAnimator;
public float getAnimProgress() {
return animProgress;
}
public void setAnimProgress(float animProgress) {
this.animProgress = animProgress;
this.invalidate();
}
public void startAnimationCircle(float size) {
progressAnimator.setFloatValues(animProgress, size);
//progressAnimator.setStartDelay(2000);
progressAnimator.start();
}
}
And the XML also.
<com.your-package.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent" />
Here's my OnDraw() method
void onDraw(Canvas canvas) {
mCanvas = canvas;
//invalidate();
int x = 0;
Iterator<Letter> it = mNextUpQueue.iterator();
while(it.hasNext()){
mCanvas.drawBitmap(it.next().getNext(), mNextUpCoordinates.get(x).x, mNextUpCoordinates.get(x).y, mPaint);
mCanvas.drawBitmap(mAvailableLetters.get(x).getNotPressed(), mAvailableLettersCoordinates.get(x).x, mAvailableLettersCoordinates.get(x).y, mPaint);
x++;
}
}
I have set canvas to a global variable mCanvas. But if I try to paint on mCanvas from outside the onDraw() method I get an error. Is it because I'm doing something wrong or the canvas must always be used from within the onDraw method?
You mustn't take the reference to the Canvas passed, as it is only valid during the onDraw(Canvas) method call.
I recommend reading http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas thoroughly, the possible ways are explained there quite well:
To the Canvas provided to the onDraw(Canvas) method, during this method call. This is done in the UI thread, so nothing complicated should be attempted here
To a Canvas you created yourself. This could be useful for preparing a bitmap in another thread and then drawing the bitmap to the Canvas given to onDraw(Canvas) method
Using a SurfaceView, and obtaining a Canvas object from it with lockCanvas() method.
You can use invalidate(); to call onDraw() and draw canvas depending on your drawing logic.
Example
public class ThumbnailImage extends android.support.v7.widget.AppCompatImageView {
public static final int FALSE = 0, TRUE = 1, NOT_SET = 2;
private int drawingStatus;
public ThumbnailImage(Context context) {
super(context);
init();
}
public ThumbnailImage(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public ThumbnailImage(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
...
drawingStatus = NOT_SET;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (drawingStatus != NOT_SET) {
if (drawingStatus == TRUE) {
...
} else {
...
}
}
}
public void setDrawingStatus(int drawingStatus) {
this.drawingStatus = drawingStatus;
invalidate();
}
}