I used the https://play.google.com/store/apps/details?id=com.davemac327.gesture.tool&hl=en
gesture tool and noticed that for a line which is top to bottom vertically is not able to detect as i am using the generated gesture file in my code as follows but not able to detect the vertical top to bottom line gesture detection
import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.Toast;
public class GesturesActivity extends Activity implements OnGesturePerformedListener {
private GestureLibrary mLibrary;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
String data="";
for (int i = 0; i < predictions.size(); i++) {
data=data+ "=="+predictions.get(i).name;
}
// We want at least one prediction
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
// We want at least some confidence in the result
if (prediction.score > 1.0) {
// Show the spell
Toast.makeText(this,data+ " "+ prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
please suggest how to perform a vertical top to bottom vertical gesture detection
There was problem with the above code which i solved by introducing
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.setGestureStrokeAngleThreshold( 90.0f);
As the default value of the angle threashold is 40.0f because of which simple vertical gestures would be skipped so changed it to 90.0f, so finally setting the GestureStrokeAngleThreshold to a value closer to 90.0f works fine
Related
I am trying to create different charts for my App and I use AndroidPlot. I want to swipe between different plots, so that when I swipe left a new chart is drawn for example.
I have implemented OnTouchListener and a function drawChart(String Discipline) that shall draw the chart, however, when drawChart is called from OnTouch, it does not work.
Moreover, if I try to detect an ACTION_MOVE, nothing happens, only ACTION_DOWN is detected. I do not see why.
Here is the code:
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.androidplot.Plot;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.PointLabelFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import com.androidplot.xy.XYStepMode;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class DrawChartsFragment extends Fragment implements View.OnTouchListener{
XYPlot plot;
public DrawChartsFragment() {
}
Number[] getXcoord(int length){
Number[] tmp = new Number[length];
for (int i = 0; i < length; i++){
tmp[i] = i;
}
return tmp;
}
private void drawChart(String Discipline){
plot = (XYPlot)getActivity().findViewById(R.id.plot);
Number[] data = Statistic.getDataPoint(Discipline);
List<Number> list = Arrays.asList(data);
Number[] x = getXcoord(list.size());
XYSeries series = new SimpleXYSeries(Arrays.asList(x),list,Discipline);
LineAndPointFormatter series1Format = new LineAndPointFormatter();
series1Format.setPointLabelFormatter(new PointLabelFormatter());
series1Format.configure(getActivity().getApplicationContext(),
R.xml.line_point_formatter_with_labels);
plot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 5);
plot.setRangeBoundaries(0, 100, BoundaryMode.FIXED);
plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 10);
plot.addSeries(series, series1Format);
plot.setTitle(Discipline);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
plot = (XYPlot)getActivity().findViewById(R.id.plot);
// plot.setOnTouchListener(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_draw_charts, container, false);
rootView.setOnTouchListener(this);
return rootView;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
drawChart("Skeet");
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
MainActivity.showToast("Move");
return true;
}
return false;
return false;
}
}
If I call drawChart("Skeet") from onActivityCreated, it works fine. But when called from OnTouchListener, then I only get a lot of messages like:
Looking up object containing: textPaint.color
Attempting to find getter for textPaint in class com.androidplot.xy.PointLabelFormatter
Invoking getTextPaint on instance of
com.androidplot.xy.PointLabelFormatter
It seems like it cannot read the parameters from the xml-file? Any ideas are appreciated!
/ Erik
Adding the following to the end of drawChart() should get things working:
plot.redraw();
You need to call Plot.redraw() when making changes to series data, add or remove series in a plot or alter display params such as line color, thickness etc. outside of onActivityCreated(). The reason it works when called from onActivityCreated() is because that code runs before Androidplot finishes initialization. Once initialization has completed, redraw() is automatically invoked one time to ensure that the plot is always visible.
Based on the answer to my previous question, I'm making a heavily-customized scrolling list, and I wish to capture the child view offset (letting the basic Android functionality handle flicking and bouncing on overscroll etc).
The offset however is always returning 0. Ideas greatly appriciated
package com.example.svexample
import android.annotation.TargetApi;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
public class ScrollViewTest {
private static final String TAG = ScrollViewTest.class.getSimpleName();
private ScrollView sv;
private View v;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public ScrollViewTest(Context context) {
v = new View(context);
v.setBackgroundColor(Color.BLUE);
v.setBottom(800); v.setRight(400);
sv = new ScrollView(context);
sv.setBackgroundColor(Color.RED);
sv.setBottom(400); sv.setRight(400);
//The next two lines don't seem to help...
//LayoutParams lp = new LayoutParams(400, 600);
//sv.setLayoutParams(lp);
sv.addView(v);
}
public void draw(Canvas canvas){
sv.draw(canvas);
v.draw(canvas);
Log.d(TAG, "Scroll is: " + sv.getScrollY());
}
}
Turns out I needed to feed my app onTouchEvent to the ScrollView in order for it to be interactive (since it was created programatically). I would have assumed the Android UI elements do this automatically but in this case apparently not.
package com.smallfan.museumexhibition.views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Toast;
public class CustomDrawableView extends View implements View.OnClickListener {
Drawable drawable;
Paint paint;
String myText;
int x1,y1;
Context context;
public CustomDrawableView(Context context, Drawable mydrawable, int x, int y, String text) {
super(context);
this.myText=text;
this.x1=x;
this.y1=y;
this.paint = new Paint();
this.context=context;
drawable=mydrawable;
drawable.setBounds(x, y, x + drawable.getMinimumWidth(), y + drawable.getMinimumHeight());
setOnClickListener(this);
}
protected void onDraw(Canvas canvas) {
drawable.draw(canvas);
}
#Override
public void onClick(View v)
{
v.setTag(myText);
Toast.makeText(context, "View clicked. "+" tag "+ v.getTag(), Toast.LENGTH_SHORT).show();
}
}
///////////////////////////////////////////////////
package com.smallfan.museumexhibition;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MapInActivity extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.v("MapInActivity", "onCreate()");
}
DrawView drawView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.v("MapInActivity", "onCreateView()");
View v = LayoutInflater.from(getActivity()).inflate(R.layout.indoormap_layout, null);
RelativeLayout maplayout=(RelativeLayout)v.findViewById(R.id.maplayout);
CustomDrawableView mCustomDrawableView = new CustomDrawableView(getActivity(),getResources().getDrawable(R.drawable.ic_launcher),10,10,"Item 1");
maplayout.addView(mCustomDrawableView);
CustomDrawableView mCustomDrawableView1 = new CustomDrawableView(getActivity(),getResources().getDrawable(R.drawable.ic_launcher),50,50,"Item 2");
maplayout.addView(mCustomDrawableView1);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Log.v("MapInActivity", "onActivityCreated().");
}
//
}
I have to draw a map like this and also show the overlay on tap of any pin like in Google map. I was able to draw views on canvas along with click listeners, but the click listener action is performed by clicking any where on screen and it's just for the view drawn in last. So if any solution then please let me know. Thanks in advance.
Have you heard of GeoFencing ? The only catch is that it works on the principle of a circle. But that should not be that bad, since you can cover the entrance with 2 circles, 1 that covers the breath of the room and includes the entrance door, and the other circle contained within the bigger circle that only covers the door.
Check out A4, I have drawn 2 circles, sorry for a ugly representation.
If a user enters the smaller circle and the bigger circle than he is still in the room. If he enters the smaller circle and exists it (and is not entering the bigger circle) he has exited the room.
Twisted but will work with minimal coding. Just read up geofencing.
Also note geofencing is now merged with LocationClient API's which consume 8 times lesser battery as compared to LocationManager. So go for it.
I'm trying to develop an android application that is basically for drawing shapes.I want the user to make gesture on the screen and the shape that is more closely matching to gesture should be drawn on the screen.
In my application,I can detect the gesture that is being performed on the screen like circle,line,rectangle etc. but there is some problem with my code.It actually detects the gesture and draws the respective shape but it happens only once.
For example. If I draw Line on the screen then line is drawn on my view but after that If I draw circle or rectangle etc then gesture is recognized but the shape is not drawn there.
Here is the full code of that
package com.pck.ShapeMaker;
import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GestureDetection extends Activity {
private GestureLibrary gLib;
private LinearLayout l1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gesture);
l1 = (LinearLayout) findViewById(R.id.playArea);
gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gLib.load())
finish();
GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
myGestureHandler handler = new myGestureHandler();
gesturesView.addOnGesturePerformedListener(handler);
}
class myGestureHandler implements OnGesturePerformedListener{
public void onGesturePerformed(GestureOverlayView gestureView, Gesture gesture){
ArrayList<Prediction> predictions = gLib.recognize(gesture);
if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
String action = predictions.get(0).name;
if ("l".equals(action)) {
Line line = new Line(getApplicationContext(),20,230,200,230);
l1.addView(line);
} else if ("r".equals(action)) {
Rectangle rect = new Rectangle(getApplicationContext());
l1.addView(rect);
Toast.makeText(getApplicationContext(), "rect", Toast.LENGTH_SHORT).show();
} else if ("t".equals(action)) {
Triangle tri = new Triangle(getApplicationContext(), 300,300,250, 350, 350, 350);
l1.addView(tri);
Toast.makeText(getApplicationContext(), "trianlge", Toast.LENGTH_SHORT).show();
}else if ("c".equals(action)) {
Circle c1 = new Circle(getApplicationContext(),50,50,30);
l1.addView(c1);
Toast.makeText(getApplicationContext(), "circle", Toast.LENGTH_SHORT).show();
}else if ("d".equals(action)) {
Toast.makeText(getApplicationContext(), "diamond", Toast.LENGTH_SHORT).show();
}
}
}
}
}
It looks like your code will produce an ever-increasing number of views within the LinearLayout. Is that what you intended? If not, you could try removing the obsolete views from the layout before adding the new one. The problem may be that there is no space in the layout for subsequent views after the first one is added.
Also, dynamically adding views like this, seems like an odd way to draw graphics. Why not define a single custom view to cover all types of drawing and include that statically in XML? Your onGesturePerformed() method would then call some method of your view to specify the type of drawing to be performed, then call invalidate() to trigger redrawing. The onDraw() method of your view would then draw whichever graphic was just specified.
I am developing a Car Race Game. I am able to move, stop, accelerate. But i want that when i press screen then car(bitmap of car image) should look like, it had jumped on its place.I am using surfaceviewto draw view
I do not want to use 3D openGL. Any help is much appreciated
finally i used.On touch event, i create a bigger bitmap and draw it on same place. So it will give zoom effect
Bitmap bitmapToZoom; // Create a Bitmap
// Now zoom it
bitmapToZoom=Bitmap.createScaledBitmap(bitmapToZoom, bitmapToZoom.getWidth()+30,bitmapToZoom.getHeight()+30, null);
//now draw it again
canvas.drawBitmap(bitmapToZoom, 0,0,null);
So now finally, Zooming actor on touch code will be like this. Class Name ZoomonTouc.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.SurfaceView;
public class ZoomonTouc extends SurfaceView {
public Bitmap mMyChracter;
public ZoomonTouc(Context context) {
super(context);
mMyChracter = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
setWillNotDraw(false);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mMyChracter, mMyChracter.getWidth()/2, mMyChracter.getHeight()/2, null);
invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
RectF rectF = new RectF(mMyChracter.getWidth()/2, mMyChracter.getHeight()/2, mMyChracter.getWidth() + 100,
mMyChracter.getHeight() + 100);
if (rectF.contains(event.getX(), event.getY())) {
mMyChracter = Bitmap.createScaledBitmap(mMyChracter,
mMyChracter.getWidth() + 10, mMyChracter.getHeight() + 10,
false);
}
return true;
}
}
To test it properly create one activity and set above surface to activity background
setContentView(new ZoomonTouc(this));