multiple rotation on ImageView - rotation

I want to rotate my ImageView 2 times
Initially it will rotate 360 degree and after that extra some degree..
I am using following code
protected void rotation(int start, int end, int time) {
Animation a = new RotateAnimation((float)start, (float)end,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
a.setDuration((long)time);
a.setInterpolator(new LinearInterpolator());
a.setFillAfter(true);
ImageView.startAnimation(a);
}
My problem is
I am calling this function 2 times so it doesn't waits for first and directly starts second rotation.
I want to wait until first rotation completes and then start second rotation

Set a Listener in first animation
a.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
callNewAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Related

how create a simple object animation in processing

QUESTION
I'm trying to create a simple object "car" animation in Processing.
My code is:
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.run();
}
public class Car implements Runnable{
private int pos_x, pos_y;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
}
public void run(){
while(true){
pos_x += 10;
print("*");
delay(200);
}
}
}
I'm expecting to see the car/rectangle move right when I click the mouse button, but nothing happens.
I've added the two print in order to see if the draw method and my car.run are executed in parallel showing some * and - printed alternately.
What I see is a sequence of - until I click and then only * are printed.
Is it possible that starting a new object thread will stop the main draw cycle?
SOLUTION
This is just a variant of the suggested solution (by Mady Daby) without using threads.
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.moving = true;
}
public class Car{
private int pos_x, pos_y;
boolean moving = false;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
//animation
if(moving){
pos_x += 10;
print("*");
}
}
}
You could make the car move right by just introducing a boolean variable to track whether the car is supposed to be moving right moving and then increment pos_x if moving is true. You can also use the clicks to toggle between moving and not moving.
Car car;
void setup() {
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10, 10);
}
void draw() {
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.toggleMoving();
}
public class Car implements Runnable {
private int pos_x, pos_y;
private boolean moving = false;
public Car(int pos_x, int pos_y) {
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void toggleMoving() {
moving = !moving;
}
public void drawCar() {
if (moving) {
this.run();
}
rect(pos_x, pos_y, 10, 10);
}
public void run() {
pos_x += 10;
print("*");
delay(200);
}
}

Unable to disable a collider

I am doing a dig the mud game. Have 2 mud gameobject(sprite) that need to dig in the correct order. I assign one script for each object. Disable mud2 boxcollider to prevent them from moving until mud 1 is drag to trigger L box collider . But when I try playing, mud2 still can be drag even though mud1 havent trigger L box collider. I also untick the box collider of mud2.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pg7_mud1 : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 startPosition;
public BoxCollider mud2;
// Start is called before the first frame update
void Start()
{
mud2.enabled = false;
}
// Update is called once per frame
void Update()
{
}
void OnMouseDown()
{
startPosition = this.gameObject.transform.position;
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
}
void OnMouseDrag()
{
Debug.Log("can drag");
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
this.transform.position = new Vector3(curPosition.x, -5f, curPosition.z);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name =="left-area")
{
Debug.Log("touched edge");
mud2.enabled = true;
}
}
You could try the Destroy(object, time) method instead of disabling colliders
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pg7_mud1 : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 startPosition;
public BoxCollider mud2;
public gameObject mud2obj;
// Start is called before the first frame update
void Start()
{
destroy(mud2);
}
// Update is called once per frame
void Update()
{
}
void OnMouseDown()
{
startPosition = this.gameObject.transform.position;
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
}
void OnMouseDrag()
{
Debug.Log("can drag");
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
this.transform.position = new Vector3(curPosition.x, -5f, curPosition.z);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name =="left-area")
{
Debug.Log("touched edge");
mud2obj.AddComponent(typeof(BoxCollider));
}
}

LibGDX - Rotate SpriteBatch on itself

I would like to rotate this SpriteBatch on itself upon clicking on a button
#Override
public void render() {
SpriteBatch batch = new SpriteBatch();
batch.begin();
batch.draw(gemTexture, 10, 10, 100, 100);
batch.end();
if (Gdx.input.isTouched()) {
rotateRight();
}
}
private void rotateRight() {
// How do I rotate it to look like
}
You're drawing a Texture using a SpriteBatch. A Texture doesn't support rotation. I suggest that the Sprite class might be better suited for what you are trying to do. Here's a rough outline of what you might do... see the Sprite javadoc for more detail.
private void createGemSprite() {
gemSprite = new Sprite(gemTexture);
gemSprite.setPosition(10, 10);
}
#Override
public void render() {
SpriteBatch batch = new SpriteBatch();
batch.begin();
gemSprite.draw(batch);
batch.end();
if (Gdx.input.isTouched()) {
rotateRight();
}
}
private void rotateRight() {
gemSprite.setRotation(gemSprite.getRotation() - 90);
}

How to make a VBox with animated "close up"?

I have built a form which has a VBox inside of a VBox. I want to make the internal VBox "close up" from the bottom to the top transitionally.
Afterwards, the next elements from the outer VBox should move up to fill the empty space, like when you remove items from a VBox.
How can I achieve this?
You can try next approach: use clipping to hide "disappearing" content and control height of the inner VBox during animation:
public class ShrinkingVBox extends Application {
private static class SmartVBox extends Region {
private Rectangle clipRect = new Rectangle();
private VBox content = new VBox();
public SmartVBox() {
setClip(clipRect);
getChildren().add(content);
}
// we need next methods to adjust our clipping to inner vbox content
#Override
protected void setWidth(double value) {
super.setWidth(value);
clipRect.setWidth(value);
}
#Override
protected void setHeight(double value) {
super.setHeight(value);
clipRect.setHeight(value);
}
// here we will do all animation
public void closeup() {
setMaxHeight(getHeight());
// animation hides content by moving it out of clipped area
// and reduces control height simultaneously
Timeline animation = TimelineBuilder.create().cycleCount(1).keyFrames(
new KeyFrame(Duration.seconds(1),
new KeyValue(content.translateYProperty(), -getHeight()),
new KeyValue(maxHeightProperty(), 0))).build();
animation.play();
}
protected ObservableList<Node> getContent() {
return content.getChildren();
}
}
#Override
public void start(Stage primaryStage) {
VBox outer = new VBox();
final SmartVBox inner = new SmartVBox();
//some random content for inner vbox
inner.getContent().addAll(
CircleBuilder.create().radius(25).fill(Color.YELLOW).build(),
CircleBuilder.create().radius(25).fill(Color.PINK).build());
// content for outer vbox, including inner vbox and button to run animation
outer.getChildren().addAll(
RectangleBuilder.create().width(50).height(50).fill(Color.GRAY).build(),
inner,
RectangleBuilder.create().width(50).height(50).fill(Color.RED).build(),
RectangleBuilder.create().width(50).height(50).fill(Color.BLUE).build(),
ButtonBuilder.create().text("go").onAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
inner.closeup();
}
}).build());
primaryStage.setScene(new Scene(new Group(outer)));
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}

How to highlight focused custom buttonfield (ImageButtonField) on Blackberry?

I made a custom ButtonField class where I have an image as a button. However, I would like to be able to select this image and to know it is selected, either by partially highlighting it or putting a square around it, whatever. I have a BitmapField in my UI that highlights itself in blue when I select it, but my other images that use ImageButtonField, do not have the blue highlight. I do not want the bitmap to disappear completely when selected.
here is the code :
package mypackage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.component.BitmapField;
public class ImageButtonField extends BitmapField{
public ImageButtonField(Bitmap image) {
super(image);
}
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
if(Characters.ENTER == character || Characters.SPACE == character) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
Any help modifying this class so it works would help immensely. I have had no success trying to make this work!
To remove default styling attributes you can add following methods:
protected void applyTheme(Graphics arg0, boolean arg1) {
}
protected void drawFocus(Graphics graphics, boolean on) {
}
You can override paint method and do paint whatever you want by checking focus status, e.g. following code will paint a red transparent layer over the bitmap image.
protected void paint(Graphics graphics) {
super.paint(graphics);
if (isFocus()) {
graphics.setGlobalAlpha(128);
graphics.setColor(0xFF0000);
graphics.fillRect(0, 0, getWidth(), getHeight());
}
}
Actually I didn't understand your question well :).

Resources