Add ImageView to FingerPaint View - android

I've created fingerpaint app. from finger paint android example API.
(SAMPLE: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/example/android/apis/graphics/FingerPaint.java)
Now i've created an function to create a bitmap from paint (and move). This is my code:
public void addProgrammatlyImage(int pos[], String filePath){
// Let's create the missing ImageView
ImageView image = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
image.setScaleType(ImageView.ScaleType.MATRIX);
Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
image.setImageBitmap(selectedImage);
image.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("TOUCH!!");
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
break;
default:
break;
}
return false;
}
});
// Let's get the root layout and add our ImageView
FrameLayout layout = (FrameLayout) findViewById(R.id.vg1);
layout.setBackgroundColor(getResources().getColor(R.color.xxx));
layout.bringToFront();
((FrameLayout) layout).addView(image, 0, params);
}
My problem is:
if inflate the new image on the same level of fingerpaint view (MyView class in the sample), disappear on the background (fingerpaint is in front of the image).
i'm unable to insert image as child of MyView. In this case there is a problem when inflate the image: classcast exception, can't cast view (MyViewClass) to ViewGroup.
Can you help me please??

Related

Setting TextView on position x,y

Hello I want to display a String on a TextView when the user touches a button. But I want this TextView to be shown on the same position where the user touches the screen. At the moment I have following code but it doesnt work out:
private void set(Integer name, Float x, Float y, Typeface font) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(10, 1, 10, 1);
RelativeLayout nlap = new RelativeLayout(this);
nlap.setLayoutParams(layoutParams);
TextView tv1 = new TextView(this);
tv1.setX(x);
tv1.setY(y);
tv1.setText("+ " + String.valueOf(name));
tv1.setTypeface(font);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(20);
nlap.addView(tv1);
}
And in the OnTouchListener:
int action = motionEvent.getAction();
int x = (int)motionEvent.getX();
int y = (int)motionEvent.getY();
set(multiplicator, Float.intBitsToFloat(x), Float.intBitsToFloat(y), font);
I hope you can help me. Thank You!
Declare textView global to that activity
TextView tv;
then in oncreate() method
tv=(TextView) findViewById(R.id.your_xml_textview);
tv.setText("your desired text");
tv.setVisibility(View.INVISIBLE);
Or you can simply use android:visibility="invisible" in your xml
Now ontouch of the screen we get the co ordinate and put the textView on that point by below code
#Override
public boolean onTouchEvent(MotionEvent event) {
int corx = (int)event.getX();
int cory = (int)event.getY();
tv.setX(corx);
tv.setY(cory);
tv.setVisibility(View.VISIBLE);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}

Bitmap from ImageView after onTouch

I have an imageview, and after i add image (a marker) on this imageview.
At the end, i want to create a bitmap, included the new marker.
But if i try to create a bitmap from relative layout, i create an image without new marker! Why?
imageview= (ImageView)findViewById(R.id.image);
//insert a marker on my imageview
final RelativeLayout rr = (RelativeLayout) findViewById(R.id.relative);
rr.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
int x = (int) event.getX() ;
int y = (int) event.getY();
RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);; //Assuming you use a RelativeLayout
ImageView iv=new ImageView(getApplicationContext());
lp.setMargins(x,y,0,0);
iv.setLayoutParams(lp);
iv.setImageDrawable(getResources().getDrawable(R.drawable.marker));
((ViewGroup)v).addView(iv);
//create a bitmap from relative layout but the new bitmap is without marker
Bitmap b1 = Bitmap.createBitmap(rr.getWidth(), rr.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b1);
((ViewGroup)v).draw(c);
}
return false;
}
It happens because adding marker dynamically to the layout, to add dynamic views it has some delay. Thats the reason creating bitmap without marker. On second touch with singe marker bitmap will be created. The solution is save bitmap after some delay.
Solution is here:
private Handler handler = new Handler(); //handler object in global
rr.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
int x = (int) event.getX() ;
int y = (int) event.getY();
RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);; //Assuming you use a RelativeLayout
ImageView iv=new ImageView(getApplicationContext());
lp.setMargins(x,y,0,0);
iv.setLayoutParams(lp);
iv.setImageDrawable(getResources().getDrawable(R.drawable.marker));
((ViewGroup)v).addView(iv);
//run handler after 1000 milli seconds i.e 1 sec
handler.postDelayed(runnable, 1000);
}
return false;
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
//wrote your block of code here
//create a bitmap from relative layout but the new bitmap is without marker
Bitmap b1 = Bitmap.createBitmap(rr.getWidth(), rr.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b1);
((ViewGroup)v).draw(c);
}
};
Look here How to Save Bitmap as image in SD card

how can we change image view background on swipe?

As a example,have to draw a layout of images.
I inflated an ImageView into a layout. When I swipe on the screen I need to change the color of the each circle to green. How should I do it. I tried with gesture detector, but I can't get what I need.
and this is my code.. I create this view by inflating an image view..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(1024);
setContentView(R.layout.test);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
count = width/30;
totalCircles = count * (height/30);
horizontalSpace = width%30;
verticalSpace = height%30;
mRelativeLayout = (RelativeLayout)findViewById(R.id.fl);
mRelativeLayout.setPadding(horizontalSpace/2, verticalSpace/2, horizontalSpace/2, verticalSpace/2);
for(int i=1;i<totalCircles+1;i++){
myButton = new ImageView(this);
myButton.setId(i);
myButton.setImageResource(R.drawable.circle_grey);
LayoutParams lp = new LayoutParams(0,0);
if(i%count != 1){
lp.addRule(RelativeLayout.RIGHT_OF, imgViews.get(i-2).getId());
if(imView != null){
lp.addRule(RelativeLayout.BELOW, imView.getId());
}
if(i%count == 0){
imView = myButton;
}
}else{
if(imView != null){
lp.addRule(RelativeLayout.BELOW, imView.getId());
}
}
mRelativeLayout.addView(myButton,lp);
myButton.getLayoutParams().width = 30;
myButton.getLayoutParams().height = 30;
imgViews.add(myButton);
myButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
System.out.println("Inside touch listener");
ImageView imV = (ImageView)v;
imV.setImageResource(R.drawable.circle_green);
}
return false;
}
});
}
}
Jack K Fouani's answer is partially correct. Instead of imagView.onTouch you need to use gridview.onTouch.
Please check this sample.
gv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentXPosition = event.getX();
float currentYPosition = event.getY();
int position = gv.pointToPosition((int)currentXPosition, (int) currentYPosition);
//using the position obtained you can change the imageview color.
}
return true;
}
});
Instead of inflating one ImageView. You should fill a GridView where every GridCell contains a circle image. Now
set the OnTouchEvent of those Gridcells to change the ImageView within the GridCell.
my advice is to use GrideView with adapter , inside the adapter you will have position for each imagView the position allow you to check if image is touched or not depending on that you can change ImageView color to white or green

android, add multiple ImageViews onTouch event

I've just started programming for Android. I've searched for my problem a lot, but the advises didn't help me. I want the same images appear on screen in the touch coordinates. That's what I've done:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View main_view = (View)findViewById(R.id.main_view);
main_view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView image = new ImageView(getApplicationContext());
//ImageView image = (ImageView)findViewById(R.id.broken);
image.setImageResource(R.drawable.broken);
image.setX(event.getX() + image.getWidth() / 2);
image.setY(event.getY() - image.getHeight() / 2);
LinearLayout top_layout = (LinearLayout) findViewById(R.id.top_layout);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
image.setLayoutParams(p);
top_layout.addView(image);
return true;
}
});
}
Everything seems right to me, but when touching the screen, nothing happens. Where is the obvious mistake I've made? Thanks in advance.
You can't do that in a LinearLayout.
Let's use a FrameLayout instead.
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
MainActivity.java
public class MainActivity extends Activity {
private FrameLayout mLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (FrameLayout) findViewById(R.id.framelayout);
mLayout.setOnTouchListener(mListener);
}
private OnTouchListener mListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_UP:
// decode the resource to get width and height
Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, opts);
int imageWidth = opts.outWidth;
int imageHeight = opts.outHeight;
// set the imageview's top and left margins
FrameLayout.LayoutParams lp = new LayoutParams(imageWidth, imageHeight);
lp.leftMargin = (int) (event.getX() - (imageWidth / 2));
lp.topMargin = (int) (event.getY() - (imageHeight / 2));
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
mLayout.addView(image, lp);
return false;
}
return false;
}
};
}
setX and setY don't set the images position. It basically scrolls the image in place. The position is controlled by its parent view. Since its parent is a linear layout, it will always be placed below or to the right of the thing above it in the layout. If you want to place it somewhere exactly, you need to put it in a parent that supports that, such as the deprecated AbsoluteLayout.

setBackgroundColor causing TableLayout alignment problems

Re-wording per previous request... I am dynamically adding TableLayouts at runtime. A view rundown looks like this... LinearLayout->ScrollView->TableLayout->Loop creating TableLayouts and TableRows at runtime. So basically I am looping and adding the following:
for(i=0.... i++)
Left Middle Right
Bottom
Now everything gets added correctly, looks correct and scrolls correctly. However I am attempting to create an onTouch event which changes the background on the touched dynamically created TableLayout. Inside the onTouch for each dynamically created TableLayout I have a onTouch event that when called does the following:
tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("OnTouch", "OnTouch Event for " + Integer.toString(v.getId()) + " event:" + Integer.toString(event.getAction()));
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("OnTouch", "Changing to GREEN");
v.setBackgroundColor(Color.GRAY);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
Log.d("OnTouch", "Changing to BLACK");
v.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
Whenever it calls the setBackgroundColor it messes up the alignment of every one of the dynamically created TableLayouts and makes them look like this:
LeftMiddleRight
Bottom
The entire code follows. I hope this is clear now.
public class TableActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
linearLayout.setWeightSum(1);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableLayout tableLayoutMaster = new TableLayout(this);
tableLayoutMaster.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// <table>
// <tr><td><table><tr><td></td><td></td><td></td></tr></table></td></tr>
// <tr><td></td></tr>
// </table>
for(int i=0; i<=30; i++)
{
TableLayout tableLayoutRow1 = new TableLayout(this);
tableLayoutRow1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tableLayoutRow1.setColumnStretchable(2, true);
//tableLayoutRow1.setBackgroundColor(Color.GREEN);
TableRow tableRow = new TableRow(this);
ImageView left_column = new ImageView(this);
left_column.setImageResource(17301550);
tableRow.addView(left_column);
TextView middle_column = new TextView(this);
middle_column.setText("Left Column:" + Integer.toString(i));
middle_column.setTypeface(null, Typeface.BOLD);
middle_column.setGravity(Gravity.LEFT);
middle_column.setPadding(5, 0, 0, 0);
tableRow.addView(middle_column);
TextView right_column = new TextView(this);
right_column.setText("Right Column:" + Integer.toString(i));
right_column.setTypeface(null, Typeface.BOLD);
right_column.setGravity(Gravity.RIGHT);
right_column.setPadding(0, 0, 5, 0);
tableRow.addView(right_column);
tableLayoutRow1.addView(tableRow);
tableRow = null;
// now for the bottom row
TextView bottom_column = new TextView(this);
bottom_column.setText("Starting activity com.developersinfo.testing.TableActivity on device emulator-5554");
bottom_column.setTextSize(12f);
bottom_column.setGravity(Gravity.LEFT);
bottom_column.setPadding(30, 0, 0, 0);
tableLayoutRow1.addView(bottom_column);
tableLayoutRow1.setId(i);
tableLayoutRow1.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
Log.d("OnFocusChange", "OnFocusChange Event for " + Integer.toString(v.getId()));
}
});
tableLayoutRow1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("OnClick", "OnClick Event for " + Integer.toString(v.getId()));
}
});
tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("OnTouch", "OnTouch Event for " + Integer.toString(v.getId()) + " event:" + Integer.toString(event.getAction()));
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("OnTouch", "Changing to GREEN");
v.setBackgroundColor(Color.GRAY);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
Log.d("OnTouch", "Changing to BLACK");
v.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
// now add to master table
tableLayoutMaster.addView(tableLayoutRow1);
}
scrollView.addView(tableLayoutMaster);
linearLayout.addView(scrollView);
setContentView(linearLayout);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
I have tried everything I can think of to keep it from doing this but so far I am unable to explain why or how to correct it. As long as I do not call the setBackgroundColor inside the onTouch everything scrolls and aligns as expected.
I had pretty much exactly the same problem you are having in a project I worked. I used long time trying to figure out what's wrong but at the end only solution was to ditch the table layout and and go for something else. It looks like a bug in the system to me.
I recommend using RelativeLayout instead if possible.
i test your code in one activity, and i don't think and don't see the alignment is bad, when touching the table row it looks good... (i mean the onTouchEvent does not mess the layout aligment.
Anyway, i suggest you a ListActivity if do so, you can set a selector background to the inner views, and a color or drawable to any state of the view, selected, pressed... and more... and inside of inner views you can use a relative layout or another layout...
There is another thing, ListActivity uses ListAdapters that are very efficient, Views are virtualized thats mean the device does not have to hold all views, only those that are visible and not the 30*4 views that you hold actually.
Thanks to both of you for the insight. I will look at the ListActivity and ListAdapters but this was more of a understanding expedition than anything at this point. I have this working using the sledgehammer approach as suggested by Juhani.
public class TestLayout extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout layout_master = new LinearLayout(this);
layout_master.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout_master.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(layout_master);
for(int i=1; i<=30; i++)
{
RelativeLayout layout_body = new RelativeLayout(this);
TextView left_Column = new TextView(this);
left_Column.setId(i);
left_Column.setText("Left");
left_Column.setGravity(Gravity.LEFT);
left_Column.setPadding(5, 0, 5, 0);
TextView middle_Column = new TextView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, left_Column.getId());
middle_Column.setLayoutParams(lp);
middle_Column.setText("Middle");
middle_Column.setGravity(Gravity.LEFT);
middle_Column.setPadding(5, 0, 5, 0);
TextView right_Column = new TextView(this);
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
right_Column.setLayoutParams(lp);
right_Column.setText("Right");
right_Column.setGravity(Gravity.RIGHT);
right_Column.setPadding(5, 0, 5, 0);
layout_body.addView(left_Column);
layout_body.addView(middle_Column);
layout_body.addView(right_Column);
TextView bottom = new TextView(this);
bottom.setId(1000 + i);
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, left_Column.getId());
bottom.setLayoutParams(lp);
bottom.setText("Starting activity com.developersinfo.testing.TestLayout on device emulator-5554");
layout_body.addView(bottom);
View view_hr = new View(this);
view_hr.setMinimumWidth(layout_body.getWidth());
view_hr.setMinimumHeight(1);
view_hr.setBackgroundColor(Color.GRAY);
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, bottom.getId());
view_hr.setLayoutParams(lp);
layout_body.addView(view_hr);
layout_body.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("OnTouch", "Changing to GREEN");
v.setBackgroundColor(Color.GREEN);
break;
default:
Log.d("OnTouch", "Changing to BLACK");
v.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
layout_master.addView(layout_body);
}
//scrollView.addView(layout_master);
setContentView(scrollView);
}
}

Categories

Resources