how can i increase the rotation speed - java

i want to increase the rotation speed.How can I increase the rotation speed of the ImageView in this code ??anyone please help me.I need help with this code. Thanks in advance.
public class MainActivity extends Activity {
ImageView img_one;
Button btn_one;
Random r;
int angle;
boolean restart=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_one = (ImageView) findViewById(R.id.imageView_ring);
r=new Random();
btn_one = (Button) findViewById(R.id.btn_1);
btn_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (restart){
angle=angle & 360;
angle=r.nextInt()+360;
RotateAnimation rotate=new RotateAnimation(angle,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotate.setFillAfter(true);
rotate.setDuration(50);
rotate.setInterpolator(new AccelerateDecelerateInterpolator());
img_one.startAnimation(rotate);
restart=false;
}else {
angle=r.nextInt()+360;
RotateAnimation rotate=new RotateAnimation(0,angle,RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotate.setFillAfter(true);
rotate.setDuration(50);
rotate.setInterpolator(new AccelerateDecelerateInterpolator());
img_one.startAnimation(rotate);
restart=true;
}
}
});
}
}

If you want to rotate faster, you need to lower the duration of the animation. Currently you have it set to last for 50 milliseconds:
rotate.setDuration(50);
but you can set the duration to whatever you need to achieve your desired effect.

Related

How can I reset my Progressbar and give it new values , when it reached its maximum?

I'm making a clicker game , where you tap a button to increase your Energy.
When my Progressbar reaches the Maximum value, it resets and sets the maximum value to the next higher one (everything works as it should be).
But when i tap again, the Progress is increased by the old maximum value but after that it continues normally.
I dont know how to fix this
public class MainActivity extends AppCompatActivity {
public int MinPoints = 0;
public int MaxPointsSSJ = 10;
public int MaxPointsSSJ2 = 20;
public int MaxPointsSSJ3 = 50;
public int Progress;
private void checker(int currentPoints, int maxPoints, int
newmaxPoints, ImageView anzeige, int bild, ProgressBar pb){
if (currentPoints == maxPoints)
maxPoints = newmaxPoints;
pb.setMax(newmaxPoints);
anzeige.setImageResource(bild);
pb.setProgress(currentPoints - maxPoints);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView Anzeige = (ImageView) findViewById(R.id.imageView);
final ProgressBar KiAnzeige = (ProgressBar) findViewById (R.id.progressBar);
Button MehrEnergie = (Button) findViewById(R.id.button);
KiAnzeige.setMax(MaxPointsSSJ);
KiAnzeige.setMin(MinPoints);
KiAnzeige.setProgress(0);
Progress = KiAnzeige.getProgress();
MehrEnergie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Progress++;
KiAnzeige.setProgress(Progress);
if (Progress == MaxPointsSSJ){
checker(Progress, MaxPointsSSJ, MaxPointsSSJ2, Anzeige, R.drawable.goku_ssj1, KiAnzeige);
}
if (Progress == MaxPointsSSJ2){
checker(Progress, MaxPointsSSJ2, MaxPointsSSJ3, Anzeige, R.drawable.goku_ssj2, KiAnzeige);
}
}
});
}
}
Set your progressbar inderminate state, So that it will show progress infinite.
KiAnzeige.setIndeterminate(true);
try this
MehrEnergie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
++Progress;
KiAnzeige.setProgress(Progress);
if (Progress == MaxPointsSSJ){
checker(Progress, MaxPointsSSJ, MaxPointsSSJ2, Anzeige, R.drawable.goku_ssj1, KiAnzeige);
}
if (Progress == MaxPointsSSJ2){
checker(Progress, MaxPointsSSJ2, MaxPointsSSJ3, Anzeige, R.drawable.goku_ssj2, KiAnzeige);
}
}
});
Your OnClick function is incrementing your progress bars progress by 1, but in your checker function, you are setting your progress bar progress to 0, effectively because currentPoints equals maxPoints.
So in OnClick, it looks like 1/10, 2/10, 3/10, ... 10/10, and then that triggers checker, which sets it to 0/20, and then it goes back to OnClick where it continues as 11/20, 12/20, 13/20, ... etc.
You can fix this by changing your maximum to the difference between the new maximum value and the old maximum value and then setting Progress back to 0, or by accounting for the difference when setting the progress.
Or, if you want the progress bar to "widen" so that the previous clicks are still shown, but the scale of the progress bar is increased, you can just set the progress to currentPoints in the checker function.

animation gets laggy after creating many objects dynamically

I have the following code, after creating ~400 ImageView instance (by calling updateUI(NewImage) ~400 times) the newly created ImageView animation would be laggy, the more time elapsed the more laggy they become. I think after anImageView animation is done, i must destroy them to avoid being laggy. any idea?
public void updateUI(final Bitmap bitmap)
{
runOnUiThread(new Runnable() {
#Override
public void run() {
final ImageView tmp = new ImageView(getApplicationContext());
tmp.setImageBitmap(bitmap);
tmp.setY(AT_THE_BOTTON_OF_ACTIVITY);
rv.addView(tmp); add the ImageView to the Relalayout
tmp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//disappear the imageView
ImageView t=imgs.get(id);
ObjectAnimator opacity = ObjectAnimator.ofFloat(tmp(The ImageView), "alpha", 0.0f);
opacity.setDuration(200);
opacity.start();
}
});
ObjectAnimator anim2 = ObjectAnimator.ofFloat(tmp, "y", 0);
anim2.setDuration(5000);
anim2.setInterpolator(new LinearInterpolator());
anim2.start();
}
});
}

A loop with setOnClickListener at Android Studio

Hey so I'm trying to make a simple code that makes the visibility of a layout visible once an image been clicked twice or more.
Sadly my code doesn't work, but I don't understand why.
Here's my code -
public class MainActivity extends AppCompatActivity {
ImageView logoIMG;
LinearLayout adminLinear;
int cnt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logoIMG = (ImageView) findViewById(R.id.logo);
adminLinear = (LinearLayout) findViewById(R.id.adminLinear);
adminLinear.setVisibility(View.INVISIBLE);
adminLinear.setEnabled(false);
while (adminLinear.getVisibility() != View.VISIBLE) {
logoIMG.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cnt >= 2) {
adminLinear.setVisibility(View.VISIBLE);
adminLinear.setEnabled(true);
} else {
cnt++;
}
}
});
}
}
}
Setting onClickListener in your while loop, will endlessly loop and keep on trying to add new Listener, which will cause issue.
Do following:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
logoIMG.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cnt >= 2) {
adminLinear.setVisibility(View.VISIBLE);
adminLinear.setEnabled(true);
} else {
cnt++;
}
}
});
}
Your code just gets stuck in an infinite loop. Setting an onClickListener once is enough. Remove the loop around setOnClickListener altogether, or at least replace it with a conditional (if).
"The first click just sets the focus to the Image then the second click actually gets handled as a click. "
try setting android:focusable="false" or true. vice versa
Check this Question

How do I randomize images in android studio? The code I wrote isn't working

I want to randomize the the back images that will be brought from a database(code still not added), but the code I wrote isn't working. I have originally just added the flip function. I searched up how to randomize images and tried to follow the code given. Sadly its not working for me.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get references to the imageView objects
imgUpperLeft = (ImageView) findViewById(R.id.imgUpperLeft);
imgUpperRight = (ImageView) findViewById(R.id.imageView2);
imgLowerLeft = (ImageView) findViewById(R.id.imageView3);
imgLowerRight = (ImageView) findViewById(R.id.imageView4);
// Setting the initial image (First face of cards)
imgUpperLeft.setImageResource(R.drawable.img_waterfall);
imgUpperRight.setImageResource(R.drawable.img_waterfall);
imgLowerLeft.setImageResource(R.drawable.img_waterfall);
imgLowerRight.setImageResource(R.drawable.img_waterfall);
imgUpperLeft.setVisibility(View.VISIBLE);
imgUpperRight.setVisibility(View.VISIBLE);
imgLowerLeft.setVisibility(View.VISIBLE);
imgLowerRight.setVisibility(View.VISIBLE);
}
ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(4) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
// Shows the second face of the cards
public void showUpperLeftImage(View v){
imgView.setImageResource(id);
}
public void showUpperRightImage(View v){
imgView.setImageResource(id);
}
public void showLowerLeftImage(View v){
imgView.setImageResource(id);
}
public void showLowerRightImage(View v){
imgView.setImageResource(id);
}
Two things in your code stand out to me:
ImageView imgView = new ImageView(this);
This ImageView is not part of the view hierarchy, meaning it is not on screen. Was it ever meant to be on screen? If not, then I don't think you need this.
public void showUpperLeftImage(View v){
imgView.setImageResource(id);
}
You have four methods like this one that change the image of the view that is not on screen. Judging by their method names, I think you want to change the image of the four ImageViews you found earlier with findViewById().
I think you want to do this:
public void showUpperLeftImage(int id) {
imgUpperLeft.setImageResource(id);
}
public void showUpperRightImage(int id) {
imgUpperRight.setImageResource(id);
}
public void showLowerLeftImage(int id) {
imgLowerLeft.setImageResource(id);
}
public void showLowerRightImage(int id) {
imgLowerRight.setImageResource(id);
}

How to rotate image in imageview on button click each time?

This is java code.I am getting image from image gallery.I have one Button and one ImageView. It is rotating only one time.When I again click button it is not rotating image.
public class EditActivity extends ActionBarActivity
{
private Button rotate;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
rotate=(Button)findViewById(R.id.btn_rotate1);
imageView = (ImageView) findViewById(R.id.selectedImage);
String path = getIntent().getExtras().getString("path");
final Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 510, 500,
false));
rotate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
imageView.setRotation(90);
}
});
}
Change your onClick() method to
#Override
public void onClick(View v)
{
imageView.setRotation(imageView.getRotation() + 90);
}
Notice, what the docs say
Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.
I'd like to update my answer to show how to use RotateAnimation to achieve the same effect in case you're also targeting Android devices running Gingerbread (v10) or below.
private int mCurrRotation = 0; // takes the place of getRotation()
Introduce an instance field to track the rotation degrees as above and use it as:
mCurrRotation %= 360;
float fromRotation = mCurrRotation;
float toRotation = mCurrRotation += 90;
final RotateAnimation rotateAnim = new RotateAnimation(
fromRotation, toRotation, imageview.getWidth()/2, imageView.getHeight()/2);
rotateAnim.setDuration(1000); // Use 0 ms to rotate instantly
rotateAnim.setFillAfter(true); // Must be true or the animation will reset
imageView.startAnimation(rotateAnim);
Usually one can setup such View animations through XML as well. But, since you have to specify absolute degree values in there, successive rotations will repeat themselves instead of building upon the previous one to complete a full circle. Hence, I chose to show how to do it in code above.

Categories