Android Drawing - android

I am learning how to code with Android but I am still confused bby the way it works, I am able to create simple draws like circles and stuff but now i want to paint the circle multiple times with a delay of 2 seconds.. I would appreciatge if you experts can help me improve my code and to put the stuff in the correct place
public class ColorChanges extends Activity {
DrawCircle dc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawCircleToCanvas()
}
void drawCircleToCanvas()
{
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dc.postInvalidate();
}
};
Thread updateUI = new Thread()
{
public void run() {
dc = new DrawCircle(this); //this line does not work
dc.setBackgroundColor(Color.WHITE);
setContentView(dc);
handler.sendEmptyMessage(0);
}
};
updateUI.start();
}
}
public class DrawCircle extends View {
Paint p1 = new Paint();
Paint p2 = new Paint();
Paint p3 = new Paint();
Paint pAll[] = new Paint[3];
public DrawCircle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DrawCircle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public DrawCircle(Context context) {
super(context);
p1.setStyle(Paint.Style.STROKE);
p1.setColor(Color.GREEN);
p1.setStyle(Paint.Style.FILL);
p2.setStyle(Paint.Style.STROKE);
p2.setColor(Color.BLUE);
p2.setStyle(Paint.Style.FILL);
p3.setStyle(Paint.Style.STROKE);
p3.setColor(Color.YELLOW);
p3.setStyle(Paint.Style.FILL);
pAll[1] = p1;
pAll[2] = p2;
pAll[3] = p3;
// TODO Auto-generated constructor stub
}
#Override
public void onDraw(Canvas canvas)
{
for (int i = 0; i < pAll.length;i++)
{
canvas.drawCircle(200, 200, 100, pAll[i]);
}
}
}

If you want to draw the same circle with its color changing over the time (each 2 seconds as you mentionned) you should use an Handler
in order to create a timer and to switch the paint at each time it is called.
Do not forget to call the invalidate function inside your custom view, as this function ask the system to redraw the screen.

Related

Drawing on Canvas, Adding Stickers, Custom Bitmaps - Android

I am looking to implement a feature that allows me to take predefined borders/images and place them over top a picture that a user has taken.
This app would be very similar to the functionality of Aviary, or Photo Editing Apps.
I cannot seem to figure out how to draw many images (say mustaches, noses, etc..) onto a canvas or view.
Is there a specific method to accomplishing this stacked bitmap/filter editing? I tried creating a custom View, but I can only set 1 setContentView();
My code is relatively simple right now, but I just wanted to know if there was a method of stacking views, or bitmaps individually.
public class CustomView extends View {
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs) {
super(context);
}
#Override
public void onDraw (Canvas canvas) {
//Draw stuff in here
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.basketball);
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
CustomView m_View = new CustomView(this);
setContentView(m_View);
}
}
This logic can be implemented many ways. It is based on your requirements and details of the application. Basically you can do something similar to this:
#SuppressLint("WrongCall")
#Override
protected void onDraw(Canvas canvas) {
// use for-int to avoid object (iterator) allocations
int count = mItems.size();
for (int i = 0; i < count; i++) {
mItems.get(i).onDraw(canvas);
}
}
ArrayList<Item> mItems;
static class Item {
Context mContext;
Drawable mDrawable;
Rect mRect;
int mResourceId;
// resource ID to point to drawable resources
public Item(Context context, int resourceId) {
mContext = context;
mResourceId = resourceId;
mRect = new Rect();
}
public void onDraw(Canvas canvas) {
// allocate image only when it actually required
if (mDrawable == null) {
mDrawable = mContext.getResources().getDrawable(mResourceId);
}
// manage mRect to place sticker and etc.
if (mDrawable != null) {
mDrawable.setBounds(mRect);
mDrawable.draw(canvas);
}
}
}

Android NoSuchMethodException thrown when using extends

I have an Activity CropActivity, that uses my own written View CropView.
CropView extends SelectorView (also selfwritten), and SelectorView extends ImageView.
When starting the CropActivity, a NoSuchMethodException is thrown.
When using the SelectorView for the CropActivity, no error is thrown, any ideas?
The Cropview class
public class CropView extends SelectorView {
public CropView(Context context) {
super(context);
initCropView();
}
private void initCropView() {
setmPaintColor(Color.WHITE);
setsMinimumSize(metrics.densityDpi);
setsTouchBuffer(metrics.densityDpi / 3);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getmLeftTop().equals(0, 0))
resetPoints();
// draw the points on the screen; one in every corner and one in the center
canvas.drawRect(getmLeftTop().x, getmLeftTop().y, getmRightBottom().x, getmRightBottom().y,
getmPaint());
canvas.drawCircle(getmLeftTop().x, getmLeftTop().y, 40, getmPaint());
canvas.drawCircle(getmLeftTop().x, getmRightBottom().y, 40, getmPaint());
canvas.drawCircle(getmRightBottom().x, getmLeftTop().y, 40, getmPaint());
canvas.drawCircle(getmRightBottom().x, getmRightBottom().y, 40, getmPaint());
canvas.drawCircle(getmCenter().x, getmCenter().y, 10, getmPaint());
}
Selectorview snippet
public SelectorView(Context context) {
super(context);
initSelectorView();
}
public SelectorView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
initSelectorView();
}
public SelectorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initSelectorView();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
/**
* Initializes the cropview and variables.
*/
private void initSelectorView() {
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeWidth(5);
mLeftTop = new Point();
mRightBottom = new Point();
mCenter = new Point();
mScreenCenter = new Point();
mPrevious = new Point();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
// set the touch point
case MotionEvent.ACTION_DOWN:
mPrevious.set((int) event.getX(), (int) event.getY());
break;
oncreate from CropActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
mContext = this;
mCropView = new CropView(this);
mFile = new File(getIntent().getStringExtra("imgpath"));
mBitmaps = new ArrayList<Bitmap>();
mTasks = new ArrayList();
mNumberOfCores = Runtime.getRuntime().availableProcessors();
mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setTitle("Enhancing image");
mBitmapDrawable = null;
mBitmapDrawable = (BitmapDrawable) Drawable.createFromPath(mFile.getAbsolutePath());
mCropView = (CropView) findViewById(R.id.image_preview);
mCropView.setImageDrawable(mBitmapDrawable);
mCropButton = (Button) findViewById(R.id.button_crop);
Custom views inflated via XML need the following constructors:
ViewName(Context)
ViewName(Context, AttributeSet)
ViewName(Context, AttributeSet, int)
You only have the first one and I guess the exception is about the second one being missing in your CropView.

Update a View through AsyncTask

I am currently writing an Android game app using GPS features of the device. I have come in contact with some problems.
Here is my Player class
public class Player {
private int xCor;
private int yCor;
public Player(int xCor, int yCor)
{
this.xCor = xCor;
this.yCor = yCor;
}
}
Here is my GameSurface View
public class GameSurface extends View{
private ArrayList<Ghost> ghostList;
private Paint paint;
public GameSurface(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public GameSurface(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public GameSurface(Context context) {
super(context);
init();
}
public void init() {
paint = new Paint();
paint.setColor(0xff00ff00); // single int in hex (Green)
// first ff = opaque; first 00 = no red
// second ff = green all the way on; second 00 = no blue
// could also do: paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
}
#Override
public void onDraw(Canvas c) {
}`
And here's my AsyncTask class
public class UpdateLocation extends AsyncTask<GameSurface,GameSurface, Integer>{
#Override
protected Integer doInBackground(GameSurface... params) {
}
#Override
protected void onPreExecute()
{
}
#Override
protected void onProgressUpdate(GameSurface... params)
{
}
`
I would like to get the GPS coordinates in the AsyncTask and use those coordinates to update the location of a circle (Player) in the View. How can I do that?
You can send the context (Activity that controls the view) to the AsyncTask:
public class AsyncTask {
protected void execute(Context context, HashMap<String, String>... params) {
...
Then in the onPostExecute method of the AsyncTask, you can call a method of the activity to update the view.
#Override
protected void onPostExecute(HashMap<String, String> result) {
((MyActivity) context).updateView(result);
}

setting onClickListener on views drawn on a canvas

So basically I have some image views drawn over my canvas, but when I try to set up onclick listeners and try to handle events, it doesn't work.
public class DrawView extends View implements OnClickListener{
Paint paint = new Paint();
RectF rf = new RectF(30, 30, 80, 80);
BallView ball = new BallView(getContext());
Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_launcher);
public DrawView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
ball.setOnClickListener(this);
ball.draw(canvas);
canvas.drawBitmap(bmp, 110, 10, paint);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Mock", Toast.LENGTH_SHORT).show();
}
}
BallView.java
public class BallView extends ImageView {
Paint b = new Paint();
public BallView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas c){
b.setColor(Color.BLUE);
c.drawCircle(50, 50, 40, b);
}
}
Basically the answer is: Your view is not receiving touch events, because it's not in views hierarchy, so click callback is also not called. Actually, view should be in View hierarchy (in other words, added to some upper level view and finally, to the window) in order to participate is user interactions.
So, if You want to implement somthing covering Your view, then it should be ViewGroup or shouldn't be related to view at all and be some abstract container.
E.g. using the way below click works without any issues (but You will need second view or modify BallView to draw bmp):
DrawView:
public class DrawView extends FrameLayout implements View.OnClickListener {
BallView ball = new BallView(getContext());
public DrawView(Context context) {
this(context, null, 0);
}
public DrawView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
addView(ball);
ball.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Mock", Toast.LENGTH_SHORT).show();
}
}
BallView class stays the same.
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ruinalst.performance.tests.views.DrawView
android:id="#+id/oscillator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>

Android Changing Colors with Thread

I am a bit frustrated as to how to do this.., Can anyone please help me or guide me in the right direction?, with the code below. I just need to make a circle change colors in 2 second intervals, and i cant find anything out there.. basically if I could understand the following it would be enough
1.I have a class call DrawCircle that Exatends View
2.I have the Activity program where I instantiate the DrawCircle Class.
Questions:
1 - Where Do I create the Thread? in Activity or in the DrawCircle class?, and if I do, what should i put in the run();
2 - Where would I put the "Thread.sleep(2000)"? 3. and Finally, what do i need to make sure I have in the DrawCircle class and what should I make sure I have in the Activity or main program?
I just need to make a circle change colors in intervals of 2 seconds
Thank you very much for all the help ( I have spent 12 hours already trying to make this work and maybe I just shouldnt do it anymore)
Here is the code
public class ColorChanges extends Activity {
DrawCircle dc;
Paint pa = new Paint();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//this line does not work
dc = new DrawCircle(this,pa);
dc.setBackgroundColor(Color.WHITE);
drawCircleToCanvas();
setContentView(dc);
}
void drawCircleToCanvas()
{
final Handler handler = new Handler()
{
public void handleMessage(Message msg) {
pa.setColor(Color.BLUE);
dc.setBackgroundColor(Color.RED);
dc.postInvalidate();
}
};
Thread updateUI = new Thread()
{
public void run() {
try {
for (int i=0;i<20;i++){
Thread.sleep(2000);
handler.sendMessage(handler.obtainMessage());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
updateUI.start();
}
}
That is the Activity and here is my Class
public class DrawCircle extends View {
Paint p1 = new Paint();
Paint p2 = new Paint();
Paint p3 = new Paint();
Paint pAll[] = new Paint[3];
public DrawCircle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DrawCircle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public DrawCircle(Context context, Paint p) {
super(context);
p1 = p;
p1.setStyle(Paint.Style.STROKE);
p1.setColor(Color.GREEN);
p1.setStyle(Paint.Style.FILL);
// TODO Auto-generated constructor stub
}
#Override
public void onDraw(Canvas canvas)
{
canvas.drawCircle(200, 200, 100, p1);
}
}
Do it in the UI thread. Instead of Thread.sleep, use Handler.postDelayed() with a two-second delay.

Categories

Resources