I have a class called examplecanvas.java that extends ImageView. The class is called by a custom view in an XML. In that XML there is a button for which I want to set an OnClick in examplecanvas.java.
I have put the OnClickListener in the OnTouchEvent, but it does not work. Here is my code:
examplecanvas.java (extending ImageView):
public class examplecanvas extends ImageView {
float xPos, yPos;
private PointF point;
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) {
Button savepoints = (Button) findViewById(R.id.btnpdf );
savepoints public void onClick(View ImageView) {
Toast.makeText(getContext(), "points saved", Toast.LENGTH_SHORT).show();
}
});
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
point = new PointF(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
point.set(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
point.set(x, y);
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
point.set(x, y);
invalidate();
break;
}
return true;
}
#Override
protected void onDraw(#NonNull Canvas canvas) {
super.onDraw(canvas);
float flat = (float)doublelat;
float flon = (float)doublelon;
canvas.drawCircle(flat, flon, 100, paint);
if (point != null) {
canvas.drawCircle(point.x, point.y, 100, paint);
canvas.save();
}
}
}
The XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.joanzapata.pdfview.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<view
class="com.myapp.trial10.examplecanvas"
android:layout_gravity="center"
android:id="#+id/yourID2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mybutton"
android:text="save"
android:id="#+id/btnpdf"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
The Activity:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import com.joanzapata.pdfview.PDFView;
public class pdfview3 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfview3);
String assetFileName = "proposed.pdf"; //The name of the asset to open
int pageNumber = 0; //Start at the first page
PDFView pdfView = (PDFView) findViewById(R.id.pdfView); //Fetch the view
pdfView.fromAsset(assetFileName) //Fill it with data
.defaultPage(pageNumber) //Set default page number
.onPageChange(null) //PageChangeListener
.enableSwipe(false)
.load();
}
}
So you want to add onClickListener for the ImageView?
-add setOnClickListener(this); in on create
-make class implement OnClickListener
-implement onClick method
-rename view in xml with View(or put directly package.examplecanvas instead of view and remove class=...)
Related
I am developing an android application, in this one page after filling up the details, at the end of the page i need to place some imageview or fragement in which user can able to sign thier signature it by touching. so that i can save that signature as image while clicking submit.
i have googled about this but i did not get any tutorial kind of thing to start.
Please help me out from this or share some link if you guys know.
Thanks in Advance,
Srikanth A
this is my class file,
package com.example.test_signature;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable.Callback;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class SignatureView extends SurfaceView implements OnTouchListener, Callback, android.view.SurfaceHolder.Callback {
public SignatureView(Context context) {
super(context);
init();
}
public SignatureView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
this.setBackgroundColor(Color.WHITE);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(3);
this.setOnTouchListener(this);
this.getHolder().addCallback(this);
}
public void setStrokeWidth(float width){
mPaint.setStrokeWidth(width);
this.invalidate();
}
public void setColor(int color){
mPaint.setColor(color);
this.invalidate();
}
public void clear(){
mDots = new ArrayList<List<Dot>>();
//To prevent an exception
mDots.add(new ArrayList<Dot>());
this.invalidate();
}
private List<List<Dot>> mDots = new ArrayList<List<Dot>>();
private class Dot{
public float X = 0;
public float Y = 0;
public Dot(float x, float y){
X = x;
Y = y;
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
//mLastActivity = Calendar.getInstance();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mDots.add(new ArrayList<Dot>());
mDots.get(mDots.size() - 1).add(new Dot(event.getX(), event.getY()));
this.invalidate();
break;
case MotionEvent.ACTION_UP:
mDots.get(mDots.size() - 1).add(new Dot(event.getX(), event.getY()));
this.invalidate();
break;
case MotionEvent.ACTION_MOVE:
mDots.get(mDots.size() - 1).add(new Dot(event.getX(), event.getY()));
this.invalidate();
break;
}
return true;
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
public void surfaceCreated(SurfaceHolder arg0) {
}
public void surfaceDestroyed(SurfaceHolder arg0) {
}
public Paint mPaint;
#Override
protected void onDraw(Canvas canvas) {
for(List<Dot> dots : mDots){
for(int i = 0; i < dots.size(); i++){
if(i - 1 == -1)
continue;
canvas.drawLine(dots.get(i - 1).X, dots.get(i - 1).Y, dots.get(i).X, dots.get(i).Y, mPaint);
}
}
}
public Bitmap getBitmap(){
Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
this.draw(c);
return b;
}
public boolean exportFile(String pathString, String fileString){
File path = new File(pathString);
path.mkdirs();
if(!fileString.toLowerCase(Locale.ENGLISH).contains(".png")){
fileString += ".png";
}
File file = new File(path, fileString);
FileOutputStream out;
try {
out = new FileOutputStream(file);
this.getBitmap().compress(Bitmap.CompressFormat.PNG, 90, out);
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
this is My XML file in which i have created the view for the user to put a signature.
<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=".SignatureView" >
<View
android:id="#+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.example.test_signature.SignatureView
android:id="#+id/signature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="18dp"
android:width="18dp" />
</RelativeLayout>
i have created this sample to test the signature functionality. But whenever i run this code, my application get crashes. Anyone please help me out. I think i have done a mistake in my XML.
#FabianCook : pla look out the code i have pasted. I'hv followed your procedure oly. but can u pls tel me wats the problem in the code ?
I have edited my XML file as you suggested, but still my application getting crashes whenever i open.
<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=".SignatureView" >
<View
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.example.test_signature.SignatureView
android:id="#+id/signature"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
public class SignatureView extends SurfaceView implements OnTouchListener, Callback {
public SignatureView(Context context) {
super(context);
init();
}
public SignatureView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
this.setBackgroundColor(Color.WHITE);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(3);
this.setOnTouchListener(this);
this.getHolder().addCallback(this);
}
public void setStrokeWidth(float width){
mPaint.setStrokeWidth(width);
this.invalidate();
}
public void setColor(int color){
mPaint.setColor(color);
this.invalidate();
}
public void clear(){
mDots = new ArrayList<List<Dot>>();
//To prevent an exception
mDots.add(new ArrayList<Dot>());
this.invalidate();
}
private List<List<Dot>> mDots = new ArrayList<List<Dot>>();
private class Dot{
public float X = 0;
public float Y = 0;
public Dot(float x, float y){
X = x;
Y = y;
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
//mLastActivity = Calendar.getInstance();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mDots.add(new ArrayList<Dot>());
mDots.get(mDots.size() - 1).add(new Dot(event.getX(), event.getY()));
this.invalidate();
break;
case MotionEvent.ACTION_UP:
mDots.get(mDots.size() - 1).add(new Dot(event.getX(), event.getY()));
this.invalidate();
break;
case MotionEvent.ACTION_MOVE:
mDots.get(mDots.size() - 1).add(new Dot(event.getX(), event.getY()));
this.invalidate();
break;
}
return true;
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
public Paint mPaint;
#Override
protected void onDraw(Canvas canvas) {
for(List<Dot> dots : mDots){
for(int i = 0; i < dots.size(); i++){
if(i - 1 == -1)
continue;
canvas.drawLine(dots.get(i - 1).X, dots.get(i - 1).Y, dots.get(i).X, dots.get(i).Y, mPaint);
}
}
}
public Bitmap getBitmap(){
Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
this.draw(c);
return b;
}
public boolean exportFile(String pathString, String fileString){
File path = new File(pathString);
path.mkdirs();
if(!fileString.toLowerCase(Locale.ENGLISH).contains(".png")){
fileString += ".png";
}
File file = new File(path, fileString);
FileOutputStream out;
try {
out = new FileOutputStream(file);
this.getBitmap().compress(Bitmap.CompressFormat.PNG, 90, out);
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
I think the code explains its self
This code takes care of everything. Has some nice little functions for different things like saving the image to a file etc.
Place this in one of your packages, and then in your XML file replace
<View android:id="#+id/id"/>
with
<package.name.SignatureView android:id="#+id/id"/>
USAGE
Create a new class in your package called SignatureView, delete everything in this file except for the first line, you should be left with package your.package.name
On the third line paste the code above and press Ctrl-Shift-O and it will import everything you need.
Save this file and then build your project.
In your XML file use:
<your.package.name.SignatureView
android:id="#+id/signature"
android:width="match_parent"
android:height="match_parent"/>
Save this and build your project.
In your activity you can use:
SignatureView mSignature;
mSignature = (SignatureView) this.findViewById(R.id.signature);
And then access the functions you need to save the signature to a file:
mSignature.exportFile("/mnt/sdcard/", "signaturefile.png");
hope this code helps you:)
esign_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.gesture.GestureOverlayView
android:id="#+id/signaturePad"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="#android:color/white"
android:clickable="false"
android:eventsInterceptionEnabled="true"
android:fadeEnabled="false"
android:gestureColor="#0000ff"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeType="multiple"
android:longClickable="false"
android:orientation="vertical"
android:uncertainGestureColor="#000000"
android:splitMotionEvents="true"
android:fadeOffset="10000000">
</android.gesture.GestureOverlayView>
<RelativeLayout
android:id="#+id/rellay_esign_donebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/DoneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done" />
<Button
android:id="#+id/ClearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Esignature.java
public class Esignature extends Activity {
GestureOverlayView gestureView;
String path;
File file;
Bitmap bitmap;
public boolean gestureTouch=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.esign_main);
Button donebutton = (Button) findViewById(R.id.DoneButton);
donebutton.setText("Done");
Button clearButton = (Button) findViewById(R.id.ClearButton);
clearButton.setText("Clear");
path=Environment.getExternalStorageDirectory()+"/signature.png";
file = new File(path);
file.delete();
gestureView = (GestureOverlayView) findViewById(R.id.signaturePad);
gestureView.setDrawingCacheEnabled(true);
gestureView.setAlwaysDrawnWithCacheEnabled(true);
gestureView.setHapticFeedbackEnabled(false);
gestureView.cancelLongPress();
gestureView.cancelClearAnimation();
gestureView.addOnGestureListener(new OnGestureListener() {
#Override
public void onGesture(GestureOverlayView arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
}
#Override
public void onGestureCancelled(GestureOverlayView arg0,
MotionEvent arg1) {
// TODO Auto-generated method stub
}
#Override
public void onGestureEnded(GestureOverlayView arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
}
#Override
public void onGestureStarted(GestureOverlayView arg0,
MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction()==MotionEvent.ACTION_MOVE){
gestureTouch=false;
}
else
{
gestureTouch=true;
}
}});
donebutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
bitmap = Bitmap.createBitmap(gestureView.getDrawingCache());
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos = new FileOutputStream(file);
// compress to specified format (PNG), quality - which is
// ignored for PNG, and out stream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
if(gestureTouch==false)
{
setResult(0);
finish();
}
else
{
setResult(1);
finish();
}
}
});
clearButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
gestureView.invalidate();
gestureView.clear(true);
gestureView.clearAnimation();
gestureView.cancelClearAnimation();
}
});
}
}
This is my updated code. It doesn't detect movement at all now. Maybe I shouldn't be making each Image an instance? Basically I want to user to be able to swipe through all the images to make them dissapear.
Thanks for all the help.
package com.picomputing.mythirdapplication;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Paul on 8/13/13.
*/
public class Pin extends ImageView implements View.OnTouchListener {
boolean isPinDown;
public Pin(Context context) {
super(context);
this.isPinDown = false;
}
public Pin(Context context, AttributeSet attrs) {
super(context, attrs);
this.isPinDown = false;
}
public Pin(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.isPinDown = false;
}
public boolean pinDown() {
return this.isPinDown;
}
public void setPinDown() {
this.isPinDown = true;
}
public void setPinUp() {
this.isPinDown = false;
}
public void togglePin() {
if (isPinDown == false)
{
isPinDown = true;
this.setImageResource(Color.TRANSPARENT);
}
else
{
isPinDown = false;
this.setImageResource(R.drawable.pin);
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX(); //--relative to mLayout--
int y = (int) event.getY(); //--relative to mLayout--
Rect r = new Rect();
view.getHitRect(r);
if(r.contains(x,y) && view instanceof ImageView){
togglePin();
}
}
return true;
}
}
You need to listen and consume ACTION_MOVE events, for the parent view of whatever you are trying to change.
Here's an example with a couple of ImageViews in a LinerLayout as a parent:
public class test extends Activity {
LinearLayout mLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
for(int i = 0 ; i < 5; i++){
ImageView iv = new ImageView(this);
iv.setImageResource(android.R.drawable.ic_dialog_info);
mLayout.addView(iv);
}
setContentView(mLayout);
mLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX(); //--relative to mLayout--
int y = (int) event.getY(); //--relative to mLayout--
Rect r = new Rect();
for(int i = 0 ; i < mLayout.getChildCount(); i++){
View v = mLayout.getChildAt(i);
v.getHitRect(r);
if(r.contains(x,y) && v instanceof ImageView){
((ImageView) v).setImageResource(android.R.drawable.ic_dialog_alert);
}
}
}
return true; //-- this means that view is interested in more events of all kinds--
}
});
}
}
I hope I didn't misunderstand your question
but if what you want to do is to prevent multitoch on the image you can add this attribute
android:splitMotionEvents="false"
in the xml in the parent view of the imageview. for example :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:splitMotionEvents="false"
>
// YOUR IMAGE VIEW HERE
</LinearLayout>
if you have any question feel free to ask in the comment :)
there are mainly three events on OnTouch action_down,Action_move and Action_up. do your coding on action down event i.e when user has touched your view. see the example here:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN) {
//write your code here
}
else {
if (arg1.getAction()==MotionEvent.ACTION_MOVE){
do things
}
else {
if (arg1.getAction()==MotionEvent.ACTION_UP){
do things
}
}
}
This is my extended View:
public class PieMenu extends FrameLayout{
private View _view;
private ArrayList<PieItem> mItems;
private Context _context;
public PieMenu(View view, Context context) {
super(context);
_context = context;
_view = view;
setWillNotDraw(false);
// TODO Auto-generated constructor stub
}
public void addPieMenu(int x, int y){
mItems = new ArrayList<PieItem>();
Path path = new Path();
path.addCircle(9, 9, 9, Path.Direction.CW);
PieItem item = new PieItem(_view,_context,path, x,y,40);
mItems.add(item);
//FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//pieView.setLayoutParams(lyp);
//addView(pieView);
// animateIn();
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
Paint paint1 = new Paint();
paint1.setColor(Color.RED);
canvas.drawCircle(50, 50, 25, paint1);
this.draw(canvas);
//_view.draw(canvas);
}
}
This is my main activity:
public class MainActivity extends Activity {
PieMenu pieMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieMenu = new PieMenu(getWindow().getDecorView().findViewById(android.R.id.content) ,MainActivity.this);
FrameLayout fl = (FrameLayout)findViewById(R.id.flayout);
fl.addView(pieMenu);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
{
pieMenu.addPieMenu(x,y);
}
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
}
This is my main activity.xml:
<FrameLayout 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:id="#+id/flayout"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<com.example.piemenu.PieMenu
android:id="#+id/circle_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
</FrameLayout>
When I compile this, the app crashes with this error:
07-20 21:38:45.906: E/AndroidRuntime(10088): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
Where am I going wrong?
You will need these constructors in your PieMenu class:
public PieMenu(View view, Context context) {
super(context);
// ~
}
public PieMenu(View view, Context context, AttributeSet attrs) {
super(context, attrs);
// ~
}
Edit 1
public PieMenu(View view, Context context, AttributeSet attrs, int style) {
super(context, attrs, style);
// ~
}
This is my MainActivity:
public class MainActivity extends Activity {
PieMenu pieMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieMenu = new PieMenu(getApplicationContext());
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
{
pieMenu.addPieMenu(x,y);
}
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
}
PieMenu:
public class PieMenu extends FrameLayout{
private Context _context;
public PieMenu(Context context) {
super(context);
_context = context;
// TODO Auto-generated constructor stub
}
public void addPieMenu(int x, int y){
Toast.makeText(_context, "text",Toast.LENGTH_LONG).show();
PieItem pieView = new PieItem(_context,x,y,2);
//FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//pieView.setLayoutParams(lyp);
addView(pieView);
invalidate();
}
}
PieItem.java:
public class PieItem extends View{
private final float x;
private final float y;
private final int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public PieItem(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
}
MainActivity.xml:
<FrameLayout 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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</FrameLayout>
Everytime I touch the scree, the toast is being called but the circle isn't being instantiated. Where am I going wrong?
easiest way to fix this, add view in onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieMenu = new PieMenu(getApplicationContext());
FrameLayout fl = (FrameLayout)findViewById(R.id.layout);
fl.addView(pieMenu);
}
and add id to your main activity xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
You aren't adding pieMenu to any of the views in your layout (R.layout.activity_main)
What does your activity_main.xml look like?
You didn't add created PieMenu to activity layout. You need to call something like this in onCreate method:
view.addView(pieMenu);
Or add your PieMenu to you activity_main layout.
I have a TextView in ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/textAreaScroller"
android:layout_width="400px"
android:layout_height="200px"
android:layout_x="0px"
android:layout_y="25px"
android:fadeScrollbars="false"
android:scrollbarSize="3px"
android:scrollbarStyle="insideOverlay" >
<TextView
android:id="#+id/scrapbook"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
</ScrollView>
<Button
android:id="#+id/upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up" />
<Button
android:id="#+id/downBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down" />
<ImageView
android:id="#+id/imageView"
android:layout_width="400px"
android:layout_height="200px"
/>
</LinearLayout>
TextView is has a lot of text that's why is scrollable. I need to draw the current visible content in TextView to Bitmap. For testing purposes I display this bitmap in ImageView. I have the following code:
public class TextviewToImageActivity extends Activity {
private TextView textView;
private ScrollView textAreaScroller;
private ImageView imageView;
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
imageView = (ImageView) findViewById(R.id.imageView);
textAreaScroller = (ScrollView) findViewById(R.id.textAreaScroller);
textView = (TextView) findViewById(R.id.scrapbook);
textView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
imageView.setImageBitmap(loadBitmapFromView(textAreaScroller));
return false;
}
});
Button upBtn = (Button) findViewById(R.id.upBtn);
Button downBtn = (Button) findViewById(R.id.downBtn);
upBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
scheduleScroller(upScroller);
imageView.setImageBitmap(loadBitmapFromView(textView));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
mHandler.removeMessages(1);
}
return true;
}
});
downBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
scheduleScroller(downScroller);
imageView.setImageBitmap(loadBitmapFromView(textView));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
mHandler.removeMessages(1);
}
return true;
}
});
loadDoc();
}
private Runnable downScroller = new Runnable() {
public void run() {
textAreaScroller.scrollBy(0, 10);
scheduleScroller(downScroller);
}
};
private Runnable upScroller = new Runnable() {
public void run() {
textAreaScroller.scrollBy(0, -10);
scheduleScroller(upScroller);
}
};
private void scheduleScroller(Runnable scrollerJob) {
Message msg = Message.obtain(mHandler, scrollerJob);
msg.what = 1;
mHandler.sendMessageDelayed(msg, 10);
}
private static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
private void loadDoc() {
String s = "";
for (int x = 0; x <= 100; x++) {
s += "Line: " + String.valueOf(x) + "\n";
}
textView.setText(s);
textView.setMovementMethod(new ScrollingMovementMethod());
}
}
The problem is that once I scroll TextView (trigger TouchEvent) the Bitmap doesn't reflect the current content of TextView and instead always has only the beginning content of TextView (it doesn't matter what's the current scroll position). I updated post to provide working code - maybe it will work on sb's other device.
UPDATE
I also tried to check WarrenFaith idea by overriding onDraw in my custom TextView but it somehow still only draw the begining content of TextView:
public class MyTextView extends TextView {
private Bitmap mBitmap;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Bitmap getBitmap() {
return mBitmap;
}
#Override
protected void onDraw(Canvas canvas) {
mBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight()
, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(mBitmap);
super.onDraw(canvas);
super.onDraw(c);
}
}
Try to override the onDraw() method of the TextView should work. There you can create a bitmap based on the canvas parameter. Details can be found in my tutorial: How to create and save a screenshot from a SurfaceView
Update:
I fixed your issue:
The activity (I changed the Handler usage and removed some methods. Basically I shrinked the code a bit).
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.ScrollingMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
/**
* #author WarrenFaith
*/
public class TextToImageActivity extends Activity {
private MyTextView textView;
private ImageView imageView;
private boolean mRepeatDrawing = false;
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.textview);
mHandler = new Handler();
imageView = (ImageView) findViewById(R.id.imageView);
textView = (MyTextView) findViewById(R.id.scrapbook);
Button upBtn = (Button) findViewById(R.id.upBtn);
Button downBtn = (Button) findViewById(R.id.downBtn);
upBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mRepeatDrawing = true;
mHandler.post(upScroller);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
mRepeatDrawing = false;
}
return false;
}
});
downBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mRepeatDrawing = true;
mHandler.post(downScroller);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
mRepeatDrawing = false;
}
return false;
}
});
loadDoc();
}
private Runnable downScroller = new Runnable() {
public void run() {
textView.scrollBy(0, 10);
imageView.setImageBitmap(textView.getBitmap());
if (mRepeatDrawing) {
mHandler.postDelayed(this, 10);
}
}
};
private Runnable upScroller = new Runnable() {
public void run() {
textView.scrollBy(0, -10);
imageView.setImageBitmap(textView.getBitmap());
if (mRepeatDrawing) {
mHandler.postDelayed(this, 10);
}
}
};
private void loadDoc() {
String s = "";
for (int x = 0; x <= 100; x++) {
s += "Line: " + String.valueOf(x) + "\n";
}
textView.setText(s);
textView.setMovementMethod(new ScrollingMovementMethod());
}
}
The custom textview: Important: the trick was to get the scrolling position!
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* #author WarrenFaith
*/
public class MyTextView extends TextView {
private Bitmap mBitmap;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Bitmap getBitmap() {
return mBitmap;
}
#Override
protected void onDraw(Canvas canvas) {
mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(mBitmap);
c.translate(0, -getScrollY());
super.onDraw(c);
super.onDraw(canvas);
}
}
The xml: (I removed the ScrollView and let the TextView handle the scrolling)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.testproject.MyTextView
android:id="#+id/scrapbook"
android:layout_width="400px"
android:layout_height="200px"
android:scrollbars="vertical"
android:scrollbarSize="3px"
android:text=""
android:background="#0000ff" />
<Button
android:id="#+id/upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up" />
<Button
android:id="#+id/downBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>