Game development - how to? - java

I am creating a game and I have assigned three image views of (balls) at the top of my screen and code an animation for them. The balls are going down to the bottom from the top, but I want the balls from the top of the screen to not show at the start of the animation... any advice?
Here is the relevant Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball1 = (ImageView) findViewById(R.id.ball1);
ball2 = (ImageView) findViewById(R.id.ball2);
ball3 = (ImageView) findViewById(R.id.ball3);
}
private void startBallAnimation() {
final TranslateAnimation animation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.86f
);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
mhandle.post(new Runnable() {
#Override
public void run() {
setBallColors();
}
});
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(final Animation animation) {}
}
}
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.example.hp.colorspoof.MainActivity">
<!-- ball imageviews -->
<ImageView
android:id="#+id/ball1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="40dp"
app:srcCompat="#drawable/ball_0" />
<ImageView
android:id="#+id/ball2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal"
app:srcCompat="#drawable/row2_2" />
<ImageView
android:id="#+id/ball3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_marginRight="40dp"
app:srcCompat="#drawable/row3_1" />
</FrameLayout>

Below is a simple untested implementation of what you want to do. I unfortunately no longer have an environment for programming Android setup, so I can't test the code for you, but this should work.
Please note that I made use of negative margins because that is the easiest way to implement it, but it isn't necessarily the best way to achieve it. See here for more: Is it a bad practice to use negative margins in Android?
To provide a summary of the changes in the XML below from what you provided:
I provided a RelativeLayout to contain your playfield. It's generally a good practice to include a FrameLayout at the top level and then use a RelativeLayout or LinearLayout below it to contain the views that will need to be manipulated. RelativeLayout and LinearLayout provide good means of placing views in relation to each other. There is also a newer type of Layout called the ConstraintLayout that I'm not personally familiar with but you may want to look at instead of RelativeLayout. See more here: What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?
I removed the android:layout_gravity statements. You don't need those with a RelativeLayout.
I utilized android:layout_alignParentTop and android:layout_marginTop to place the balls above the top of your layout by exactly the size of the ball.
I utilized android:layout_marginLeft, android:layout_centerHorizontal, and android:layout_marginRight to place the balls horizontally on the screen as you did.
XML below:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.example.hp.colorspoof.MainActivity">
<RelativeLayout
android:id="#+id/play_area"
android:layout_width="match_parent"
android:layout_height"match_parent">
<!-- ball imageviews -->
<ImageView
android:id="#+id/ball1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="-30dp"
android:layout_marginLeft="40dp"
app:srcCompat="#drawable/ball_0" />
<ImageView
android:id="#+id/ball2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="-30dp"
app:srcCompat="#drawable/row2_2" />
<ImageView
android:id="#+id/ball3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="-30dp"
android:layout_marginRight="40dp"
app:srcCompat="#drawable/row3_1" />
</RelativeLayout>
</FrameLayout>

Related

setBackgroundColor is crashing the android application

I recently made a small app that should change the background color & text of the application when a certain amount of money is generated (here 100000). However, whenever I go past 10000, the application crashes. I am not sure why and can not get around it. I saw that this problem is with background color only because it is otherwise displaying text correctly.
Here is the XML for activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/Trial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EC5353"
tools:context=".MainActivity">
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:gravity="center"
android:text="#string/Title"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="#string/moneyPresent"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Title" />
<Button
android:id="#+id/moneyGenerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:background="#FFFFFF"
android:backgroundTint="#732424"
android:backgroundTintMode="multiply"
android:text="#string/generateMoneyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/money" />
<TextView
android:id="#+id/richTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="#string/grind_boi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/moneyGenerator" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Java Code (Omitted the packages as it would take a lot of space. Do tell me if anyone wants to read it.)
public class MainActivity extends AppCompatActivity {
private Button makeMoney;
private int totalMoney=0;
private TextView moneyText;
private TextView richAlert;
private ConstraintLayout background;
NumberFormat currency=NumberFormat.getCurrencyInstance(new Locale("en","in"));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalMoney+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d("MoneyTag", "Money Made: "+totalMoney);
Toast.makeText(getApplicationContext(),"Total Money= \u20B9"+totalMoney,Toast.LENGTH_LONG)
.show();
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
background.setBackgroundColor(16776960);
}
}
});
}
}
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
You're finding all the views, except background(ConstraintLayout), that's why it is throwing NullPointerException (Most likely, because you haven't attached logs).
You need to find the layout first
background = findViewById(R.id.Trial);
in onCreate.
you haven't defined "background" view in the above.
to use .setBackgroundColor you need to use it with some view
try making object of layout view then change the color of that layout
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.Trial);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
background = findViewById(R.id.Trial);
just find layout and null pointer excpetion will removed

Layering images in Android Studio?

Currently I have a UI designed with imageviews, the problem is they cannot overlap or be layered. (Or if they can, I haven't figure out how)
I'd like to do 3 layers, the bottom layer is the board,
The next layer is pieces,
The next layer is a selected piece with valid moves.
My idea is that I will have listeners on the top layer only.
I am just looking for the best data structure to use for this, the app is a chesslike game I am working on developing.
If there is another approach that might be better I am all ears as well.
Thanks!
As requested code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Activity"
android:id="#+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView22"
android:src="#drawable/ni_whitesquare"
android:layout_below="#+id/textView5"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="76dp"
android:contentDescription="whitesquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView32"
android:src="#drawable/ni_blacksquare"
android:layout_alignTop="#+id/imageView22"
android:layout_alignLeft="#+id/imageView22"
android:layout_alignStart="#+id/imageView22"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:contentDescription="blacksquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView22"
android:layout_toLeftOf="#+id/imageView22"
android:layout_toStartOf="#+id/imageView22" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView21"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_below="#+id/imageView22"
android:layout_toRightOf="#+id/imageView12"
android:layout_toEndOf="#+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView23"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_above="#+id/imageView12"
android:layout_toLeftOf="#+id/imageView33"
android:layout_toStartOf="#+id/imageView33" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView31"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignBottom="#+id/imageView21"
android:layout_alignLeft="#+id/imageView32"
android:layout_alignStart="#+id/imageView32" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView21"
android:layout_toLeftOf="#+id/imageView21"
android:layout_toStartOf="#+id/imageView21" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView13"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView23"
android:layout_alignLeft="#+id/imageView12"
android:layout_alignStart="#+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView33"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView23"
android:layout_alignLeft="#+id/imageView32"
android:layout_alignStart="#+id/imageView32" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/directions"
android:id="#+id/directionsstring"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView21"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Startingpoint"
android:src="#drawable/ni_pawn"
android:layout_above="#+id/imageView11"
android:layout_toLeftOf="#+id/imageView12"
android:layout_toStartOf="#+id/imageView12" />
</RelativeLayout>
Activity :
public class GameActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView mStarting = (ImageView) findViewById(R.id.Startingpoint);
final ImageView mCenterSq = (ImageView) findViewById(R.id.imageView22);
final ImageView m12 = (ImageView)findViewById(R.id.imageView12);
mCenterSq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
m12.setImageResource(R.drawable.ni_blacksquare);
mCenterSq.setImageResource(R.drawable.ni_portal);
//center square turns into a portal when clicked.
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()) &&
mCenterSq.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_portal).getConstantState()) )
{
mCenterSq.setOnClickListener(null);
return;
}
mStarting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//Pawn selected
mStarting.setImageResource(R.drawable.ni_whitesquare);
m12.setImageResource(R.drawable.ni_greensquare);
mCenterSq.setImageResource(R.drawable.ni_greensquare);
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()))
{
mStarting.setOnClickListener(null);
}
m12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (m12.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_greensquare).getConstantState()))
{
m12.setImageResource(R.drawable.ni_pawn);
mCenterSq.setImageResource(R.drawable.ni_portal);
}
}
});
}
});
};
});
}
}
For layering views, you might want to use a FrameLayout see this answer: Layout Layers? Z-Axis?
Also, if you're trying to set up a layout in which you have several consecutive views that need to be lined up one next to the other, the most appropriate element is usually a LinearLayout. LinearLayout will automatically set up your views in consecutive order in the order you declared them, according to the android:orientation attribute you set for it.
Overall, I'd suggest looking at the docs for each of these. RelativeLayout is probably not the way to go since it involves a lot of managing of view positioning which just seems frivolous for what I think you are trying to do. I hope this helps

Creating a new instance of an Imagebutton with a button click

The app I am programming has a menu in which the user can pick a new building to build. The user will press "Buy" I would like this to create a new instance of the imagebutton that corresponds to that particular building. My current problem is that I only understand how to do this by setting up these in the xml file and setting them invisible until they buy it. I would like to dynamically create these buildings when they are bought. Is there a way to dynamically create an instance of an image button is this way? I don't really think including my source code is necessary here since I just need an example that works (most examples I have seen just don't work out). But if you would like to see some source code let me know.
Any help is appreciated. Thanks.
Code inside buy building button:
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setImageResource(R.drawable.mainhut);
newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(newColonyHut);
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="0dp"
android:layout_marginRight="20dp" >
<Button
android:id="#+id/buyColonyHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_below="#+id/colonyHutPrice" />
</LinearLayout>
You can create ImageView and dynamically add to layout you specified.
sample:
in oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(your_image_here);
image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
test.addView(image);
}
});
the activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer"
android:text="Button" />
</LinearLayout>
As you can see ImageView is created when the button is clicked in the layout and then add it to the LinearLayout dynamically.
The LayoutParams is where you set the height and width of your ImageView respected the its parent which is the LinearLayout

onClick on an imageview within a listview within a dialog

I can't seem to get an onClick event on an imageview from a class that extends Fragment. What am i doing wrong here? I'm relatively new to developing for android..
Currently, this code gives me a null pointer exception - when i add the onClickListener code
Here is my code:
ImageView mImageView = (ImageView) v.findViewById(R.id.option_icon);
mImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
these icons are placed in a listview which is placed on a Dialog:
Dialog's list view and button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/preset_list_view"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="#string/ok"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"/>
</LinearLayout>
items within the list view (imageview and textview):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/option_icon"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dp" />
</RelativeLayout>
It must be that the ImageView you're trying to find it outside the scope of whatever v refers to.
If the image view is inside a list view then you should add the onClickListener from within the adapter
The imageView that you are trying to find is not accessible from v. If the imageView is in the main layout then use findViewById without using v else first inflate the view and then use view.findViewById.

Android TranslateAnimation to Center

I need to apply a translation animation to an ImageView, this ImageView is positioned at certain place and i need to animate it to the center of the screen. Here is my translate xml code, but it doesn't animate the ImageView to the center.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="50%p"
android:toYDelta="50%p" />
</set>
This is the XML where the ImageView is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/WelcomeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/welcome_background"
android:orientation="vertical" >
<ImageView
android:id="#+id/welcome_innovaLogo"
android:layout_width="300dp"
android:layout_height="150dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="1dp"
android:layout_marginTop="5dp"
android:contentDescription="#string/welcome_innovaLogoString"
android:scaleType="fitXY"
android:src="#drawable/innova_logo" >
</ImageView>
<TextView
android:id="#+id/welcome_innovaInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/welcome_innovaLogo"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/welcome_innovaLogo"
android:text="#string/welcome_innovaInfoString"
android:textColor="#9acd32"
android:textStyle="bold" />
<TextView
android:id="#+id/welcome_innovaHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/welcome_innovaInfo"
android:layout_marginRight="20dp"
android:text="#string/welcome_innovaHeaderString"
android:textColor="#9acd32"
android:textSize="20sp"
android:textStyle="italic" />
<ListView
android:id="#+id/welcome_commandsListView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome_innovaLogo"
android:layout_marginTop="40dp"
android:cacheColorHint="#00000000" />
<LinearLayout
android:id="#+id/welcome_loadingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:orientation="horizontal"
android:visibility="invisible" >
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:indeterminateDrawable="#drawable/progress_large" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:text="Loading Data..."
android:textColor="#color/green_color"
android:textSize="19sp" >
</TextView>
</LinearLayout>
<!--
<fragment
android:id="#+id/welcome_facebookFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/welcome_innovaLogo"
android:layout_marginLeft="20dp"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_toRightOf="#+id/welcome_commandsListView"
class="innovaEgypt.com.Welcome.FacebookFragment" >
</fragment>
-->
Have a look at this question. I gave a similar answer but the animation is not from an xml but from code. Hope it helps.
EDIT:
Your problem is that the animation doesn't take into account the image dimensions. The animation is centering the origin coordinate of your ImageView.
I still suggest my solution.
I tried the code in the answer that I linked and it works. Just copy this function:
private void moveViewToScreenCenter( View view )
{
RelativeLayout root = (RelativeLayout) findViewById( R.id.WelcomeLayout );
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics( dm );
int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
int originalPos[] = new int[2];
view.getLocationOnScreen( originalPos );
int xDest = dm.widthPixels/2;
xDest -= (view.getMeasuredWidth()/2);
int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
and call it passing your view as the parameter:
ImageView img1 = (ImageView) findViewById( R.id.welcome_innovaLogo );
moveViewToScreenCenter(img1);
If your image still disappear, maybe is because it is behind your other widgets, like the ListView. If that is the case, reestructure your xml layout definig the ImageView at the end, just after the Listview (just cut and paste the ImageView).
For future updates...
Apply view property animation to move position of widget. Image view in this case
int layoutWidth = parentLayout.getWidth();
layoutWidth = ((int) layoutWidth / 2) - (imageView.getWidth()) ;
Log.d(TAG, " position - " + layoutWidth);
imageView.animate()
.translationX(-layoutWidth)
.translationY(0)
.setDuration(500)
.setInterpolator(new LinearInterpolator())
.start();
Try adding delay to this code.
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
moveViewToScreenCenter( view );
}
},
1000);

Categories

Resources