thank you so much for reading. I have a textView that you can pinch zoom, and when I add "textview.setMovementMethod(new ScrollingMovementMethod());" to my code I can also scroll the zoomed textview which is exactly what I need. The problem is as soon as the text is zoomed, only the scrolling works but the pinch zoom doesnt work anymore, (unless I place one of my fingers outside the textview and the other inside). Any help will be really appreciated. I got the code from here: Pinch Zooming TextView
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView scaleGesture;
ScaleGestureDetector scaleGestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleGesture = (TextView)findViewById(R.id.article);
scaleGesture.setText("this is some text");
scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
public class simpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float size = scaleGesture.getTextSize();
Log.d("TextSizeStart", String.valueOf(size));
float factor = detector.getScaleFactor();
Log.d("Factor", String.valueOf(factor));
float product = size*factor;
Log.d("TextSize", String.valueOf(product));
scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = scaleGesture.getTextSize();
Log.d("TextSizeEnd", String.valueOf(size));
return true;
}
}
}`
I just resolve this problem (with the same example) only add one "if" in my code:
#Override
public boolean onTouchEvent(MotionEvent event) {
// get how many fingers is in touch (in that case, if are more
// than 1, the user don't want do the scroll
if (event.getPointerCount() > 1) {
scaleGestureDetector.onTouchEvent(event);
return true;
}
return false;
}
Related
I want to get the gray scale value of a pixel in OpenCV for Android. The problem is that OpenCV for Android is very much undocumented and a lot of the methods are different. I have found answers to this problem, but none of them work for android.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.hardware.camera2.CameraCaptureSession;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
public class testActivity extends AppCompatActivity implements CvCameraViewListener2 {
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status){
switch (status){
case LoaderCallbackInterface.SUCCESS:
{
mOpenCvCameraView.enableView();
break;
}
default:
{
super.onManagerConnected(status);
}
}
}
};
private JavaCameraView mOpenCvCameraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.testVideoView);
mOpenCvCameraView.setMaxFrameSize(240,320);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
getSupportActionBar().hide();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
public void onResume(){
super.onResume();
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
#Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
#Override
public void onCameraViewStarted(int width, int height) {
}
#Override
public void onCameraViewStopped() {
}
#Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return inputFrame.gray();
}
}
I'll assume you mean "how to extract gray pixel from RGB Mat", because getting the gray pixel from Mat that represents a grayscale image is trivial.
What you can do is define submat of size 1x1
for instance
Mat bgrPixel= rgbMat.submat(x,y,x,y).clone();
Mat grayPixel = new Mat(1,1, CvType.CV_8UC1);
Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY);
(maybe the indexes are x,y,x+1,y+1, I'm not sure)
after that you can use your gray pixel.
It's not an elegant solution but it should work.
I'm trying to setup a way to only accept one gesture at a time then after that gesture is done do the next in line. So for example I want to start with a single tap then after you input the tap it would move on to the next one being a double tap. Here's the code I'm using:
import android.content.DialogInterface;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
public class secondscreen extends ActionBarActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private TextView mainMessage; //variable refers to strings.xml// private TextView InText;
private TextView startButton;
private GestureDetectorCompat gestureDetector; // Secondary Variable used for gestures
private DialogInterface.OnClickListener OnClickListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondscreen2);
mainMessage = (TextView) findViewById(R.id.mainMessage);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}
///////// GESTURE METHODS //////////
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
mainMessage.setText("single tap");
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
mainMessage.setText("double tap");
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
mainMessage.setText("double tapping event");
return false;
}
#Override
public boolean onDown(MotionEvent e) {
mainMessage.setText("down");
return false;
}
#Override
public void onShowPress(MotionEvent e) {
mainMessage.setText("showing the press");
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
mainMessage.setText("tap up");
return false;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
mainMessage.setText("scrolling is fun");
return false;
}
#Override
public void onLongPress(MotionEvent e) {
mainMessage.setText("very long presses");
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
mainMessage.setText("flinging");
return false;
}
///////// GESTURE METHODS //////////
//this next block is needed to run gestures//
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_secondscreen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I specifically want the gestures to not happen at the same time as it is set up now, the app will run the gestures and display a text but it does it all at the same time displaying the last one pressed - I want to know how to set it up on one screen so that it asks for a gesture and moves on to the next gesture after that one is pressed preferably waiting a second so it doesn't happen instantly. I'm pretty sure it has something to do with an if else statement but I have no idea how to word it.
One simple way I can think of is to just use an enum combined with if statements
enum ex.
private enum EVENT{
SINGLE_TAP,DOUBLE_TAP,EVENT_3,EVENT_4 ...;
}
private EVENT currentEvent=EVENT.SINGLE_TAP;
Event Handler
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (currentEvent==Event.SINGLE_TAP){
mainMessage.setText("single tap");
currentEvent=Event.DOUBLE_TAP;//set to next desired event
return true;
}
return false;
}
Do something similar in your other event handlers, on the last event i would make it loop back to the first
If you desire a delay between the events implement a stopwatch and check if your desired time has passed between events. Something like so.
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(stopWatch.getTime > lastEventTime+delay){//pseudo code
if (currentEvent==Event.SINGLE_TAP){
mainMessage.setText("single tap");
currentEvent=Event.DOUBLE_TAP;//set to next desired event
return true;
}
}
return false;
}
I've created 2 separate projects in Android from tutorials etc., - one which runs an animation of an horse running, and the other a scrolling background. Both work well - but because they implement separate Activities, how do I manage to run both inside one project?? ie the animation of the horse on the top of the scrolling background?
Here is my code for the separate projects:
FrameAnimateActivity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class FrameAnimatedActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animated);
ImageView gyroView = (ImageView) findViewById(R.id.gyro);
//set the animation drawable as background
gyroView.setBackgroundResource(R.drawable.gyro_animation);
//create an animation drawable using the background
AnimationDrawable gyroAnimation = (AnimationDrawable) gyroView.getBackground();
//start the animation
gyroAnimation.start();
ImageView img_animation = (ImageView) findViewById(R.id.gyro);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(5000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.frame_animated, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the code for the scrolling background:
MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MovingBackGround(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MovingBackground.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MovingBackGround extends SurfaceView implements
SurfaceHolder.Callback {
private Bitmap backGround;
public MovingBackGround(Context context) {
super(context);
backGround = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ab);
setWillNotDraw(false);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
doDrawRunning(canvas);
invalidate();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
/**
* Draws current state of the game Canvas.
*/
private int mBGFarMoveX = 0;
private int mBGNearMoveX = 0;
private void doDrawRunning(Canvas canvas) {
// decrement the far background
mBGFarMoveX = mBGFarMoveX - 1;
// decrement the near background
mBGNearMoveX = mBGNearMoveX - 4;
// calculate the wrap factor for matching image draw
int newFarX = backGround.getWidth() - (-mBGFarMoveX);
// if we have scrolled all the way, reset to start
if (newFarX <= 0) {
mBGFarMoveX = 0;
// only need one draw
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
} else {
// need to draw original and wrap
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
canvas.drawBitmap(backGround, newFarX, 0, null);
}
}
}
I am working in Eclipse and struggling with a certain step in my assignment so any help would be appriciated because I actually dont know how to do it even though I realise how glaringly simple it probably is...
I have a shopping app and I have created my CheckoutActivity... now... it needs to start when the user clicks the checkout button in my CartActivity.
I need to add code in the ocCreate method of my CartActivity (which I will provide below) that specifies the listener for the Checkout button. then I need to define a button variable named btn inside the onCreate. I then need to assign the button element from the view using the id that you specified in my activity_cart.xml layout.
All I have so far is this... and I have no idea what to do...
btn.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
//I cant figure out how to get it to start the CheckoutActivity
}
});
Just in case you need to see the code I have so far... here is the code I have for my cartActivty
package uk.ac.uk.st265.shopper;
import java.text.DecimalFormat;
import java.util.List;
import java.util.ResourceBundle.Control;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class CartActivity extends Activity {
CartListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
TextView text = (TextView) findViewById(R.id.total_price); // its not in
// XML
DecimalFormat df = new DecimalFormat("#.00");
text.setText("£"
+ df.format(((ShopperApp) getApplication()).getCartTotal()));
adapter = new CartListAdapter(this);
ListView cartList = (ListView) findViewById(R.id.cart_list);
adapter.setItemList(((ShopperApp) getApplication()).cart);
cartList.setAdapter(adapter);
// Show the Up button in the action bar.
setupActionBar();
//work5ass2part6
//btn.setOnClickListener(new OnClickListener() {
//public void onClick(final View v) {
// I cant figure out how to get it to start the CheckoutActivity
//}
//});
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cart, menu);
return true;
}
public class CartListAdapter extends BaseAdapter {
private final Context context;
private List<Product> itemList;
public CartListAdapter(Context c) {
context = c;
}
public void setItemList(List<Product> itemList) {
// this.itemList = itemList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View cell = convertView;
if (cell == null) {
// get layout from mobile xml
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
cell = inflater.inflate(R.layout.adapter_cart, parent, false);
}
Product p = itemList.get(position);
// set value into text view according to position
TextView textView = (TextView) cell
.findViewById(R.id.product_title);
textView.setText(p.getProductName());
textView = (TextView) cell.findViewById(R.id.product_info);
textView.setText("Price " + p.getPrice());
// set value into image view according to position
ImageView imgView = (ImageView) cell
.findViewById(R.id.product_image);
// clear the image
imgView.setImageDrawable(null);
// and load from the network
p.loadImage(imgView, 54, 54);
return cell;
}
public List<Product> getItemList() {
return itemList;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
case R.id.show_cart:
// Create the intent for the cart activity
Intent intent = new Intent(getApplicationContext(),
CartActivity.class);
startActivity(intent);
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here is the code I have so far on my CheckoutActivity:
package uk.ac.uk.st265.shopper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class CheckoutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.checkout, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
// myIntent.putExtra("key", value); //if you want to pass parameter
CurrentActivity.this.startActivity(myIntent);
I'm trying to test something out, but all I need is an activity that blinks between black and white screens at an interval of 1 second. I've looked at postDelayed, but I'm still not sure how I would implement that. This is what I have right now, but it doesn't "blink"
MainActivity:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Blink extends Activity {
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blink);
while (i == 0) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
changeColor();
}
}, 1000);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void changeColor() {
if (findViewById(R.id.mylayout).getBackground().equals("#ffffff")) {
findViewById(R.id.mylayout).setBackgroundResource(Color.BLACK);
} else {
findViewById(R.id.mylayout).setBackgroundResource(Color.WHITE);
}
}
}
Updated Code:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Blink extends Activity {
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blink);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
changeColor();
}
}, 1000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void changeColor() {
if (i == 0) {
findViewById(R.id.mylayout).setBackgroundColor(Color.WHITE);
i++;
} else if (i == 1) {
findViewById(R.id.mylayout).setBackgroundColor(Color.BLACK);
i--;
}
}
}
Update 2:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Blink extends Activity {
int i = 0;
Boolean bool = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blink);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
changeColor();
}
}, 1000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void changeColor() {
if (bool) {
findViewById(R.id.mylayout).setBackgroundColor(Color.WHITE);
bool = false;
} else {
findViewById(R.id.mylayout).setBackgroundColor(Color.BLACK);
bool = true;
}
}
}
I narrowed down the exact question I should be asking right here: Why doesn't this postDelayed run forever?
To fix this code, all I had to do was add handler.postDelayed(this, 1000); inside of run.