Hello to all!
I have not participated much in the forum, though I have been reading you for a while So... I am going to ask for your help
I'm trying to make a custom paint with the finger (to draw, to take notes, whatever) and I'm having some troubles.
Up to now, from what I have read, I'm using a class extended from View implementing OnTouchListener , and using the onDraw() method, with the onTouch event. I detect when the screen is touched (pressed, or dragged to "paint" in the screen) with the onTouch method, store the last point in an array, and in the onDraw method, I paint all the array.
-As my knowledge goes, in the onDraw method, you have to re-paint all the canvas (this is translated to my problem, to repaint all the stroke or "painting"). Is this correct or I am doing anything wrong? Is there any way to only update the current canvas, and not have to re-paint all the screen? Maybe using othe classes or other methods, but I have found nothing in the net (maybe I'm not looking in the correct places).
- The second problem I found is that when I drag the finger across the screen, sometimes the ontouch method doesen't capture all the points I have "touched" with the finger. I mean, I don't want to capture ALL the points, but when I drag the finger with moderate speed (not very fast) there are important gaps in between. Is there a way to optimize this and try to capture more points in between?
So far, this is the code I have relevant to the issues I'm explaining:
I'm trying to copy the code but I get a error (It sais I can not post outside links, but I'm not posting any links, just the code)
Code:
private ArrayList<PointStroke > stroke= new ArrayList<PointStroke >();
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
int signLength = stroke.size();
PointStroke pf;
PointStroke pfant;
for (int index = 0; index < signLength; index++) {
pf = stroke.get(index);
canvas.drawCircle(pf.x,pf.y,7,p);
if (index > 0 ) {
pfant = stroke.get(index-1);
canvas.drawLine(pfant.x, pfant.y, pf.x, pf.y, p);
}
}
canvas.drawCircle(x,y,7,p);
invalidate();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
pr = event.getPressure();
sz = event.getSize();
long now = System.currentTimeMillis();
stroke.add(new PointStroke(x,y,pr,sz,now));
return true;
}
private class PointStroke{
public float x;
public float y;
public float pr;
public float sz;
public long time;
public PointStroke(float newX, float newY, float newPR, float newSZ, long newTIME) {
x = newX;
y = newY;
pr = newPR;
sz = newSZ;
time = newTIME;
}
}
Thanks for all your answers and the time to read and answer!
Related
Can someone make a .cab or an .exe program to calculate Weight Watchers points? I found this article on the internet where it looks like someone as already done it but they did not post it. Here is the article
http://geekswithblogs.net/cdahlinge...-mobile-meets-weight-watchers--mvp-style.aspx
Craig Dahlinger
<< Presenting at Richdmond code camp 2008.2 | Home | mshtml β the ongoing adventure >> windows mobile meets weight watchers : MVP style Ok, so I know it has been a long time since a post, but it has been really busy with work and family. I have been busy coding and learning lots of new stuff. I work with a great bunch of developers and my current team lead is a great mentor.
Well for the new year the wife and I decided to get back into shape. I started hitting the gym and so did she but she is also doing weight watchers with a friend. One of the things they do is they have to calculate points on a daily basis. These points are comprised of calories, fat and fiber. There is a formula for these three which in turn results in the number of points a particular item is. A few months ago I convinced the wife to get a windows mobile device (woo hoo!) and she is a good power user. So one night she asks me, βIs there a way I can just enter in the calories, fat and fiber on my phone and it tell me how many points something is?β. I did some searching and there are numerous online versions of the calculator but no native ones for windows mobile. I found the formula here, and started to get to work.
I wanted to approach this application using the MVP design pattern. I know it may be overkill for this simple of an application but I thought it would be good practice.
I started with the interface for the data model, in this case it would be the main caloric properties of food.
namespace WWPC.Common.Interfaces{ public interface IFoodModel { int Fiber { get; set; } int Calories { get; set; } float Fat { get; set; } int Points { get; set; } int CalculatePoints(); }}I then wrote up the interface for the view for the model.
namespace WWPC.Common.Interfaces{ public interface IFoodCalcView { int Calories { get; } int Fiber { get; } float Fat { get; } int Points { set; } event EventHandler DataChanged; }}Next, came the interface for the presenter.
public interface IFoodCalcPresenter { void OnCalculatePoints(); }
Ok, now that I got my main interfaces in place, time to code up the implementation. I started with the model first since this was the class that would provide the implementation for calculating the caloric points. Using the formula mentioned above, the CalculatePoints() method came out like so:
public int CalculatePoints(){ var calories = Convert.ToDecimal(Calories); var cal = calories / 50; var totalFat = Convert.ToDecimal(Fat); var fat = totalFat / 12; var fiber = Convert.ToDecimal(Fiber); return Points = Convert.ToInt32(Math.Round(cal + fat - (fiber/5), 0)); } With the model complete, I then moved to the presenter. The presenter would be responsible for binding the model to the view responding to the data changes in the view and rebinding those changes to the model. I made the presenter with an overloaded constructor to take a view and a model. The presenter then binds to the data changed event on the view which enables the presenter to update the model from the view. The OnCalculatePoints() method will update the view with the points value after using the model for calculation.
namespace WWPC.Common{ public class FoodPresenter : IFoodCalcPresenter { private readonly IFoodCalcView _View; private readonly IFoodModel _Model; public FoodPresenter(IFoodCalcView view, IFoodModel model) { _View = view; _View.DataChanged += new EventHandler(_View_DataChanged); _Model = model; } void _View_DataChanged(object sender, EventArgs e) { SetModelFromView(); } private void SetModelFromView() { _Model.Calories = _View.Calories; _Model.Fat = _View.Fat; _Model.Fiber = _View.Fiber; } #region IFoodCalcPresenter Members public void OnCalculatePoints() { _View.Points = _Model.CalculatePoints(); } #endregion }}
With the presenter done it was time to implement the view. I wanted a simple mobile form where you can enter in data quickly and then calculate the results. I initially tried using a label to display the result, but did not like it. I then tried a mobile gauge control, but that took up too much space on the small screen. Finally I decided to use the notification class for windows mobile. I did not use the managed wrapper version, I used the the version created by Christopher Fairbairn, found here. This version has an awesome implementation which exposes many features of the notification class. I wanted to give the user the ability to dismiss the notification when they were done reading the results. Also using the notification class the UI was able show the needed text boxes for entry and the SIP panel along with the results without needing to scroll the screen. Here is a screen shot of the main form.
Now with the controls in place on the form, I can implement the view. The form creates a new presenter and passed into it a new model during construction. When the calculate menu option is clicked the main form raises the data changed event then calls the OnCalculateMethod on the presenter. When the presenter binds the model to the view, during the set of the points value, the notification is shown to the user via the ShowNotification method.
namespace WWPC.Calc{ public partial class WWPCalculator : Form, IFoodCalcView { private readonly FoodPresenter _Presenter; private NotificationWithSoftKeys _Notification; public WWPCalculator() { InitializeComponent(); _Presenter = new FoodPresenter(this,new FoodModel()); } public int Calories { get { return (string.IsNullOrEmpty(txtCalories.Text)) ? 0 : Int32.Parse(txtCalories.Text); } } public int Fiber { get { return (cmbFiber.Text == "4 or more") ? 4 : (string.IsNullOrEmpty(cmbFiber.Text)) ? 0 :Int32.Parse(cmbFiber.Text); } } public float Fat { get { return (string.IsNullOrEmpty(txtFat.Text)) ? 0 : float.Parse(txtFat.Text); } } public int Points { set { ShowPointsNotification(value); } } public event EventHandler DataChanged; private void mnuExit_Click(object sender, EventArgs e) { this.Close(); } private void mnuCalculate_Click(object sender, EventArgs e) { if (DataChanged != null) this.DataChanged(sender, e); _Presenter.OnCalculatePoints(); } private void mnuClear_Click(object sender, EventArgs e) { txtCalories.Text = string.Empty; txtFat.Text = string.Empty; cmbFiber.Text = "0"; } private void ShowPointsNotification(int points) { _Notification = new NotificationWithSoftKeys { Text = String.Format("Total Points:{0}", points), Caption = "Weight Watchers Point Calculator", RightSoftKey = new NotificationSoftKey(SoftKeyType.Dismiss, "Dismiss"), }; _Notification.RightSoftKeyClick+=new EventHandler(_Notification_RightSoftKeyClick); _Notification.Visible = true; } void _Notification_RightSoftKeyClick(object sender, EventArgs e) { if (_Notification == null) return; _Notification.Visible = false; _Notification = null; } }}
Now, when it is all put together, it looks like so.
Below is a link to the source code. The project was done using Visual Studio 2008 against the windows mobile 5 sdk. It will also work against windows mobile 6 sdk, I just chose version 5 since that is the common sdk. Thanks for reading!!
I have a little problem with my equations while using a spinner. What happens is user chooses option from spinner then enters three separate numbers in different EditText the calculation will perform as soon as the user hits the calculation button.
the originally return of the calculation is 0.0. until you again choose an option from the spinner and click calculate again. which then returns the correct answer.
thanks ahead of time.
Is there supposed to be a question in there somewhere?
Yeah sorry,
I guess I forgot to add the question.
Does anyone now why this is happening? should I put the onItemSelectionListener before the onClick. that part of the code looks somethink this.
Code:
btnCalc.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if ((ageEdit.getText().toString()== " " )
||(ageEdit.getText().length()== 0 )
||(heightEdit.getText().toString()== " ")
||(heightEdit.getText().length()== 0 )
||(weightEdit.getText().toString()== " ")
||(weightEdit.getText().length()== 0 )
)
{
showErrorAlert("Some Input are empty!",
input.getId());
}
else {
height = Double.parseDouble(txtwlHeight2.getText().toString());
weight = Double.parseDouble(txtwlWeight.getText().toString());
age = (int) Double.parseDouble(txtageinput01.getText().toString());
spnGender.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onNothingSelected(AdapterView<?> arg0){}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub{
switch (position){
case 0:
//some equation
break;
case 1:
//some equation
break;
}
}
});
// etc. such calculation out to text view.
Any Idea's anyone? I've does a lot of research and reading. Tried to re write code in different ways still no luck.. need help please.
Hey all,
So Im a newbie learning Java and I am trying to take what I have learned and complete a simple project I came up with. Im just working working on a basic BMI calculator. I have to issues.... I cant get it to print the result and my scanner shows a output code when I run the program. Could somebody take a look and give me advice?
import java.util.Scanner;
public class calcBMI {
public static void main(String[] args) {
Scanner height = new Scanner(System.in);
startC startCObject = new startC();
System.out.println("Enter your height:");
System.out.println(height);
String temp = height.nextLine();
startCObject.setHeight(temp);
Scanner weight = new Scanner(System.in);
System.out.println("Enter your Weight:");
System.out.println(weight);
String temp1 = weight.nextLine();
startCObject.setWeight(temp1);
Scanner result = new Scanner(System.in);
System.out.println("BMI:" + result);
}}
Click to expand...
Click to collapse
and
public class startC {
//HEIGHT
private String enterH;
public void setHeight(String height) {
enterH = height;
}
public String getHeight() {
return enterH;
}
//WEIGHT
private String enterW;
public void setWeight(String weight) {
enterW = weight;
}
public String getWeight() {
return enterW;
}
//RESULT
private String ht, wt;
public void resHeight(String height) {
ht = height;
}
public String resheight() {
return ht;
}
//then
public void resWeight(String weight) {
wt = weight;
}
public String resweight() {
return wt;
}
//then
public String result; {
result = ht + wt;
}
public String getResult(String result) {
return result;
}
}
Click to expand...
Click to collapse
I don't think you can print a Scanner?
Also, you can use just one Scanner for both inputs, and it is good to also close them with scannerName.close(); when finished.
Try and get that sorted, then see what happens? Please post your code in the
Code:
and
tags too, then .
bassie1995 said:
I don't think you can print a Scanner?
Also, you can use just one Scanner for both inputs, and it is good to also close them with scannerName.close(); when finished.
Try and get that sorted, then see what happens? Please post your code in the
Code:
and
tags too, then .
Click to expand...
Click to collapse
Ah; sorry about that, and thank you for the info.
I actually ended up tearing the code down and rethinking it. I was making it far more difficult than it needed to be.
Here is what I have now which is working perfect. Pretty excited considering this is the first project I've done on my own.!
Code:
import java.util.Scanner;
public class theCalc {
public static void main(String[] args) {
// feet
System.out.println("Enter your height (Feet):");
Scanner scan = new Scanner(System.in);
double feet = scan.nextDouble();
// inches
System.out.println("Enter your height (Inches):");
double inches = scan.nextDouble();
// weight
System.out.println("Enter your weight:");
double weight = scan.nextDouble();
// convert to inches
double height = feet * 12 + inches;
// height squared
double htsqr = height * height;
// BMI formula
double htInchByWeight = weight / htsqr;
double BMI = htInchByWeight * 703;
// BMI =
System.out.println("Your BMI is: " + BMI);
}
}
Was thinking it to be a bit too complicated, but a good exercise with multiple classes and such. It looks much better now, and glad you got it working!
Sent from my GT-I9300 using Tapatalk 4 Beta
I've been trying to learn to program for Android lately by simply coming up with ideas and implementing them. It started out easy enough with pulling information from an online RSS feed and showing this in a nice environment. But the current idea has me stumped.
I'd like the following to happen:
Take a picture using intent
Entire picture is shown in a new activity
Zoom in on a certain spot
Add predefined items to the picture
Press next which connects the items from left to right
Add some more items
Press next to connect the new items
Zoom out
Save the image
First taking a picture, this wasn't too hard using the camera intent:
Code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
I can then extract the absolute path from fileUri with the following line:
Code:
String path = new File(fileUri.getPath()).getAbsolutePath();
This path can be put in a bundle which can be put in an intent which is then used to start the activity that should show the image.
Code:
public class TestActivity extends Activity implements SurfaceHolder.Callback {
private static final String TAG = TestActivity.class.getSimpleName();
private String path = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceView view = new SurfaceView(this);
setContentView(view);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
path = bundle.getString("path");
view.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.d(TAG,"Can't draw because canvas is null");
}
else {
Bitmap bitmap = BitmapFactory.decodeFile(path);
Paint paint = new Paint();
if(bitmap == null) {
Log.d(TAG,"Can't draw because bitmap is null");
}
else {
canvas.drawBitmap(bitmap,0,0,paint);
}
holder.unlockCanvasAndPost(canvas);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
The first issue here is that it of course doesn't show the entire photo. Not that surprising considering it's larger than the view. Ideally I'd like to zoom out to show the entire photograph. Once I've zoomed one way I'd assume that zooming in on the part that you want should also be possible.
Next is adding the objects. My idea was simply catching any touch events and adding a new object once the finger is released. This way I'd end up with a list of items with each having a draw function which can be called through the surfaceview when it is redrawn.
Connecting these items could simply be done by creating a line object and going through the list of all items and using their locations for the begin and endpoints of the lines
One of the big issues here is that the x and y locations would be relative to the screen, not to the photo. Which would mean that when you zoom back out the entire background would change but the actual items would remain at the same spot and in the same size.
I've been searching and searching for any tutorial or other question about the same issue, but either I've had no luck or I've been using the wrong keywords. And for all I know everything I have up till now could be wrong.
If anyone could give some pointers, or maybe knows a guide or tutorial somewhere or some better keywords I could use for searching I'd really appreciate it.
Xylon- said:
One of the big issues here is that the x and y locations would be relative to the screen, not to the photo. Which would mean that when you zoom back out the entire background would change but the actual items would remain at the same spot and in the same size.
Click to expand...
Click to collapse
would you just not need to either control or track the sample/scale if the image so that you know the 1st pixel displayed (top left) and the scale factor, then the eventX/Y can be processed to be relative to what you want ?
As the title suggests.
I have seen this in xperia z2 lockscreen and wanted to recreate that effect
I googled arround but only found some which also needed me to create a game of it which i dont want
I want to have a view and can simply move my finger in it and let sparkles,particles,smoke etc drag under it
Anyone knows how?
Sent from my C5303 using XDA Free mobile app
Create a view (sparkles or whatever) and at ontouch listener update the parameters of X & Y
I made this in a previous app of mine (Floata) but you can adapt it to the app
here you are :
Adding the view :
Code:
twitterp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
twitterp.gravity = Gravity.TOP | Gravity.LEFT;
twitterp.x = 0;
twitterp.y = 0;
windowManager.addView(twitter, twitterp);
Code:
twitter.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = twitterp.x;
initialY = twitterp.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return false;
case MotionEvent.ACTION_UP:
return false;
case MotionEvent.ACTION_MOVE:
twitterp.x = initialX
+ (int) (event.getRawX() - initialTouchX);
twitterp.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(twitter, twitterp);
return false;
}
return false;
}
});
}
this is a floating object (like a chathead of Fb messenger) , you can use the samew concept in your app i think