How to blend two pics by accessing each pixel? - java

For my Android application in java I need to blend 2 pics by accessing each pixel on both photos and getting the average of the pixels to give a blending effect. I know how to access each pixel but I don't know if the way I figured the average for the blending is correct.
this is my code:
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
private Bitmap bmp;
private ImageView img2;
private Bitmap bmp2;
private Bitmap operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.imageView1);
BitmapDrawable abmp = (BitmapDrawable)img.getDrawable();
bmp = abmp.getBitmap();
img2 = (ImageView)findViewById(R.id.imageView2);
BitmapDrawable abmp2 = (BitmapDrawable)img2.getDrawable();
bmp2 = abmp2.getBitmap();
}
public void blend(View v){
operation= Bitmap.createBitmap(bmp.getWidth(),
bmp.getHeight(),bmp.getConfig());
for(int i=0; i<bmp.getWidth(); i++){
for(int j=0; j<bmp.getHeight(); j++){
int p = bmp.getPixel(i, j);
int p2 = bmp2.getPixel(i, j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
int r2 = Color.red(p2);
int g2 = Color.green(p2);
int b2 = Color.blue(p2);
r=(int)(r+r2)/2;
g=(int)(g+g2)/2;
b=(int)(b+b2)/2;
img2.setVisibility(View.INVISIBLE);
operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
}
}
img.setImageBitmap(operation);
}
And the layout goes as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.blendpics.MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:src="#drawable/pic1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:src="#drawable/pic2" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="blend"
android:text="blend" />

Have a look at using RenderScript and the built in intrinsic for blend, ScriptIntrinsicBlend: http://developer.android.com/reference/android/renderscript/ScriptIntrinsicBlend.html.

Related

Setting Up Horizontal RecyclerView

I want to add 10dp padding for my first tile in recycler View.
Just Like in the Book My show app the first App has 10dp padding (assume) then the padding between two tiles (suppose 5dp).
Similarly Last tile also have 10dp (assume) padding in the end
I'm adding my code of the recycler view If anyone wants to add something in it
RecyclerMetroAdapter.java
package com.example.android.indianmetro;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mPlaceNames = new ArrayList<>();
private ArrayList<String> mPlaceImageUrl = new ArrayList<>();
private ArrayList<String> mPlaceMetroDistance = new ArrayList<>();
private ArrayList<String> mClosestMetro = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context context, ArrayList<String> placeNames, ArrayList<String> placeImageUrl,
ArrayList<String> placeMetroDistance, ArrayList<String> closestMetro ) {
mPlaceNames = placeNames;
mPlaceImageUrl = placeImageUrl;
mPlaceMetroDistance = placeMetroDistance;
mClosestMetro = closestMetro;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onCreateViewHolder: called");
Glide.with(mContext)
.asBitmap()
.load(mPlaceImageUrl.get(i))
.into(viewHolder.placeImage);
viewHolder.placeName.setText(mPlaceNames.get(i));
viewHolder.placeMetroDistance.setText(mPlaceMetroDistance.get(i));
viewHolder.closestMetro.setText(mClosestMetro.get(i));
}
#Override
public int getItemCount() {
if (mPlaceNames.size() < 8){
return mPlaceNames.size();
} else return 8;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView placeImage;
TextView placeName;
TextView placeMetroDistance;
TextView closestMetro;
public ViewHolder(#NonNull View itemView) {
super(itemView);
placeImage = (ImageView) itemView.findViewById(R.id.image);
placeName = itemView.findViewById(R.id.textView);
placeMetroDistance = itemView.findViewById(R.id.textView2);
closestMetro = itemView.findViewById(R.id.placeMetroName);
}
}
}
Code of tiles I'm using inside my RecyclerView
horizontal_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/delhi1" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="2dp"
android:ellipsize="end"
android:maxLines="1"
android:text="India Gate"
android:textColor="#color/darkHeading"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="36dp"
android:text="2.4 Km Away"
android:textColor="#color/blueAccent"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="24dp"
android:src="#drawable/ic_metro"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:id="#+id/placeMetroName"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Central Secretariat"
android:textColor="#color/subHeading"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
</android.support.constraint.ConstraintLayout>
Also if some one can suggest that how to make a tile fully visible from left and not allow any tile partially hidden from the left side
I guess you can try to set the padding in onBindViewHolder(),
for the first:
if (i == 0)
viewHolder.placeImage.setPadding(10, 5, 5, 5);
and for the last:
if (i == mPlaceImageUrl.size() - 1)
viewHolder.placeImage.setPadding(5, 5, 10, 5);
Change the numbers to what you like they are left->top->right->bottom padding.

Is there an Android equivalent to ActionListener?

What I essentially need is an action listener that notes if another touchable views have been clicked so that they can be set to enabled (i.e., if imageview1 is clicked, imageview2 can now be clicked whereas before it could not). I was looking around and found ActionListener and ActionEvents, but after importing all necessary packages, updating my SDK, and syncing my gradle files, Android Studio still doesn't recognize those options. I can't seem to find a way to get it to work. Is there something I'm missing in getting them to resolve, or is there another kind of listener I can use?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:id="#+id/sq2"
android:background="#color/preFocus"
android:onClick="imagePress"
android:layout_centerVertical="true"
android:nextFocusDown="#+id/sq3"
android:nextFocusUp="#+id/sq1"
android:nextFocusLeft="#+id/sq4"
android:nextFocusRight="#id/sq4"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="250dp"
android:background="#color/preFocus"
android:id="#id/sq4"
android:onClick="imagePress"
android:enabled="false"
android:layout_centerVertical="true"
android:nextFocusDown="#id/sq3"
android:nextFocusUp="#id/sq1"
android:nextFocusLeft="#id/sq2"
android:nextFocusRight="#id/sq2"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="400dp"
android:background="#color/preFocus"
android:id="#id/sq3"
android:onClick="imagePress"
android:enabled="false"
android:layout_centerHorizontal="true"
android:nextFocusDown="#id/sq1"
android:nextFocusUp="#id/sq1"
android:nextFocusLeft="#id/sq2"
android:nextFocusRight="#id/sq4"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:background="#color/preFocus"
android:id="#id/sq1"
android:onClick="imagePress"
android:enabled="false"
android:layout_centerHorizontal="true"
android:nextFocusDown="#id/sq3"
android:nextFocusUp="#id/sq3"
android:nextFocusLeft="#id/sq2"
android:nextFocusRight="#id/sq4">
<requestFocus />
</ImageView>
</RelativeLayout>
Java:
package com.example.mcken.spinnertest;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.view.menu.MenuPopupHelper;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.PopupMenu;
import java.lang.reflect.Field;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
final int childcount = rl.getChildCount();
for (int i = 0; i < childcount; i++){
final View square = rl.getChildAt(i);
final View nextSquare = rl.getChildAt(i+1);
square.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent click){
nextSquare.setEnabled(true);
}
});
}
}
public void clickableChange(){
//what am I trying to do
//set an actionlistener
//if this is clicked
//the "next" one is clickable
// RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
// final int childcount = rl.getChildCount();
//
// for (int i = 0; i < childcount; i++){
// final View square = rl.getChildAt(i);
// final View nextSquare = rl.getChildAt(i+1);
// square.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent click){
// nextSquare.setEnabled(true);
// }
// });
// }
}
public void imagePress(View test){
int id = test.getId();
ImageView picture = (ImageView) findViewById(id);
final int pictureId = id;
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
public boolean onMenuItemClick(MenuItem selection){
ImageView thisThing = (ImageView) findViewById(pictureId);
Drawable icon = selection.getIcon();
thisThing.setImageDrawable(icon);
return true;
}
});
popup.show();
}
});
}
}

Read View's children while initializing View

This View is supposed to make a Button for each LinearLayout inside itself, and put that LinearLayout inside a ScrollView. I think the problem is that the LinearLayouts don't exist yet, since they are created AFTER initializing this view. Therefore getChildCound() returns zero. You could work around this by hard coding the number of Buttons or getting it from a XML attribute, but you can't put the LinearLayouts in the ScrollViews, when there are no LinearLayouts. Is there a way to work around this?
code:
package mika.actual;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;
public class AccordionWidget extends LinearLayout{
public AccordionWidget(Context c, AttributeSet attrs) {
super(c, attrs);
final Context context = c;
final int count = this.getChildCount();
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);
String[] btns = getResources().getStringArray(R.array.buttons);
ArrayList<LinearLayout> lls = new ArrayList <> (count);
for(int i = 0; i < count; i++){
View child = this.getChildAt(i);
if (child instanceof LinearLayout) {
lls.add((LinearLayout)child);
}
}
for(int i = 0; i < lls.size(); i++){
if(btns[i] == null){
Log.e("error: ", "Please add more Button lables to the strings.xml file.");
}
final Button btn = new Button(context);
final LinearLayout ll = lls.get(i);
final ScrollView sv = new ScrollView(context);
final int col_pressed = a.getColor(R.styleable.accordion_backgroundColorPressed, Color.BLACK);
final int col_unpressed = a.getColor(R.styleable.accordion_backgroundColorUnpressed, Color.YELLOW); //Black and yellow, black and yellow...
LayoutParams btnparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams swparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams llparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
btn.setText(btns[i]);
btn.setBackgroundColor(col_pressed);
llparams.weight = 1f;
btn.setLayoutParams(btnparams);
ll.setLayoutParams(llparams);
sv.setLayoutParams(swparams);
ll.setOrientation(LinearLayout.VERTICAL);
sv.setVisibility(View.GONE);
this.addView(sv);
this.addView(btn);
sv.addView(ll);
btn.setOnClickListener(new OnClickListener() {
private boolean btnstate = false;
#Override
public void onClick(View v) {
if (btnstate) {
btn.setBackgroundColor(col_pressed);
sv.setVisibility(View.VISIBLE);
btnstate = true;
} else {
btn.setBackgroundColor(col_unpressed);
sv.setVisibility(View.GONE);
btnstate = false;
}
}
});
}
a.recycle();
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<mika.actual.AccordionWidget
xmlns:accordion="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
accordion:text_color="#color/text_color"
accordion:backgroundColorPressed="#color/button_pressed"
accordion:backgroundColorUnpressed="#color/button_not_pressed"
android:id="#+id/swaggy"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
</mika.actual.AccordionWidget>
The idea is that you need to put your logic into onLayout
Something like this:
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final Context context = c;
final int count = this.getChildCount();
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);
String[] btns = getResources().getStringArray(R.array.buttons);
ArrayList<LinearLayout> lls = new ArrayList <> (count);
for(int i = 0; i < count; i++){
View child = this.getChildAt(i);
if (child instanceof LinearLayout) {
lls.add((LinearLayout)child);
}
}
.......
See this
Make sure that you call child.layout() for each child inside this method.
You can create custom LinearLayout inside ScrollView:
<ScrollView>
<LinearLayout android:id="#+id/parent"/>
</ScrollView>
Then Create java class for your layout and inflate it.
See this

Create a clear button to clear canvas android

I can't get my clear button to work in my program. My circle button works which displays random circles but my clear button does nothing. I'm lost. I know i'm supposed to use drawColor.Color.TRANSPARENT or the mode.clear but nothing is working.
package com.example.randomcircles;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.*;
public class DisplayRandomCircles extends Activity
{
FrameLayout f1, f2;
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.activity_display_random_circles);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
f1 = (FrameLayout) findViewById(R.id.frame);
}
#SuppressLint("WrongCall")
public void doit(View v)
{
int rand = (int) (Math.random() * 60);
DrawCircle c = new DrawCircle(getApplicationContext());
Canvas d = new Canvas();
Paint p = new Paint();
switch (v.getId())
{
case (R.id.btn1):
f1.addView(c);
break;
case (R.id.btn2):
d.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
break;
}
}
}
package com.example.randomcircles;
import java.util.Random;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.*;
import android.view.*;
public class DrawCircle extends View
{
Random random = new Random();
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
int randX = random.nextInt(700);
int randY = random.nextInt(1000);
int randR = random.nextInt(200);
int color = Color.rgb(red, green, blue);
public DrawCircle(Context con)
{
super(con);
}
#SuppressLint("DrawAllocation")
#Override
public void onDraw(Canvas c)
{
super.onDraw(c);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(100);
p.setColor(color);
p.setStyle(Paint.Style.FILL);
c.drawCircle(randX, randY, randR, p);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".75"
android:orientation="vertical" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="bottom|center"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight=".50"
android:onClick="doit"
android:text="#string/Circle" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:layout_gravity="end|bottom"
android:onClick="doit"
android:text="#string/Clear" />
</LinearLayout>
</LinearLayout>
You are creating a new instance of DrawCircle every time doIt is called.
The old instance is still there. So you need to either remove the old view from f1, or get the exact same instance of DrawCircle you created before, set the new color, and then call invalidate() on the DrawCircle to force the redraw.

Drawing Circles using Canvas and Framelayout android

I am trying to create an app that displays a circle everytime I click a button. I have the layout looking great but when i click the button(circle) to display a circle on the screen nothing happens. I'm not confident that my draw circle class is being called correctly in my main activity. Here is my code below.
package com.example.randomcircles;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class DisplayRandomCircles extends Activity
{
DrawCircle c;
Canvas d;
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.activity_display_random_circles);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
c = new DrawCircle(getApplicationContext());
d = new Canvas();
FrameLayout f1 = (FrameLayout) findViewById(R.id.frame);
}
#SuppressLint("WrongCall")
public void doit(View v)
{
switch (v.getId())
{
case (R.id.btn1):
c.onDraw(d);
break;
case (R.id.btn2):
break;
}
}
}
Here is my DrawCircle class
package com.example.randomcircles;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawCircle extends View
{
public DrawCircle(Context con)
{
super(con);
}
#Override
protected void onDraw(Canvas c)
{
super.onDraw(c);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(100);
p.setColor(Color.RED);
p.setStyle(Paint.Style.FILL);
c.drawCircle(75, 75, 100, p);
}
}
And my layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".75"
android:orientation="vertical" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="bottom|center"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight=".50"
android:onClick="doit"
android:text="#string/Circle" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:layout_gravity="end|bottom"
android:onClick="doit"
android:text="#string/Clear" />
</LinearLayout>
</LinearLayout>
Ok, here are some changes I'd make for this. I'm not exactly sure what you're trying to do, but this should make things easier.
First, change your class "DrawCircle" like this:
public class DrawCircle extends View
{
final Paint circlePaint;
{
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setAntiAlias(true);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(100);
circlePaint.setColor(Color.RED);
circlePaint.setStyle(Paint.Style.FILL);
}
public DrawCircle(Context con)
{
super(con);
}
public DrawCircle(Context con, AttributeSet set)
{
super(con, set);
}
public DrawCircle(Context con, AttributeSet set, int style)
{
super(con, set, style);
}
#Override
protected void onDraw(Canvas c)
{
super.onDraw(c);
c.drawCircle(75, 75, 100, circlePaint);
}
}
This will allow you to reuse the same Paint object with each draw because the onDraw() method can be called hundreds of times and there's no need to slow down your program for this.
Second, adding the other constructors allows you to add the View to your FrameLayouts by xml.
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.randomCircles.DrawCircle
android:id="#+id/circleFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Next, to fill your screen, you need to loop in your onDraw method. Think about drawing on the canvas as a stamp. Every time you draw, you stamp your image at the location you specify on top of the previous draw.
So
protected void onDraw(Canvas c)
{
super.onDraw(c);
for(int i = 0; i < 10; i++)
{
c.drawCircle(i*100, 75, 100, circlePaint);
}
}
This should draw 10 circles of radius 100 pixels across the top of your screen.

Categories

Resources