I am working on android application in which on button click i am giving width and height to my linear layout to make it full screen and by clicking again i am giving its width and height to 1dp. I just need to animate this thing like when i press the button it will slowly animate to full screen and on again pressing of button it will slowly animate to 1dp width and height. I have used translate for this thing but it didn't work in this scenario.
btnResize.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(resize==true){
otherLayout.setLayoutParams(new LayoutParams(1, 1));
resize=false;
}else{
resize=true;
int height = LayoutParams.WRAP_CONTENT;
int width = LayoutParams.MATCH_PARENT;
// ImageView imageView = (ImageView)findViewById(R.id.imageView);
//
// Animation animZoomin = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in);
//
// imageView.startAnimation(animZoomin);
otherLayout.setLayoutParams(new LayoutParams(width, height));
}
}
});
You can use scale animation:
Start by calculating your screen size: Get screen dimensions in
pixels
Next get the current view width and height.
Calculate the ratio between the 2 to get the scale factor (hint: screenH / viewH)
Next use view.animate().scaleX(xRatio).scaleY(yRatio) to start the
animation
You may need to translate the view too if it is not centered in the screen
Other option is to use this method: Expand/collapse animation
Related
I am making a video player android application like MX player. what i want to do is same like in MX player.For example:If the user clicks on the aspect ratio button it should set the video size to 100%. if the user again clicks the button it should set (aspect ratio or scale type to be crop.on next click it should set to stretch. again it should set it to fit to screen etc.
i have developed other logic of the player except this button. i will handle the clicks logic on the aspect ratio button too.
just tell me what code i will need to write to change the video size not the surface view size when the user selects the button.
Note: I am not using video view at all. i am assigning surface view a media player through surface holder. and by default my video plays in full screen
I have done it finally after some research. if anyone is looking for an answer then it is here for you. it is not the correct size though but it is for an example. the set Text is not according to the width and height but it is just to explain you the functionality
you can get the screen width and height and +/- the amount you want to increase or decrease in width and height whatever you want.
above your activity, after imports you write this to initialize:
android.view.ViewGroup.LayoutParams lp;
This piece of code will get the device width and height.
public void getDeviceWidthAndHeight(){
lp = surfaceView.getLayoutParams();
screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
}
And this piece of code will set the height and width you give to it.
#Override
public void onClick(View v) {
int id=v.getId();
if (id==R.id.resize_video){
if (clickCount==0){
getDeviceWidthAndHeight();
lp.width = screenWidth-50;
lp.height = screenHeight-50;
surfaceView.setLayoutParams(lp);
resizeVideo.setText("100%");
clickCount=1;
}
else if (clickCount==1){
getDeviceWidthAndHeight();
lp.width = screenWidth-300;
lp.height = screenHeight-100;
surfaceView.setLayoutParams(lp);
resizeVideo.setText("Full Screen");
clickCount=2;
}
else if(clickCount==2){
getDeviceWidthAndHeight();
lp.width =screenWidth;
lp.height = screenHeight;
surfaceView.setLayoutParams(lp);
resizeVideo.setText("100%");
clickCount=3;
}
else if(clickCount==3){
getDeviceWidthAndHeight();
lp.width =screenWidth;
lp.height = screenHeight-500;
surfaceView.setLayoutParams(lp);
resizeVideo.setText("Fit to Screen");
clickCount=0;
}
}
}
I got a layout in which there's a RelativeLayout with a visibility of GONE. This rl is a layout for a bar with buttons which appears at the bottom of the fragment when setting the visibility to visible. While the RL is still not visible, there are 2 buttons and when I set it to visible, the RL is covering the buttons.
What I want to do is simply move the buttons up above that bar which becomes visible. What I tried to do it:
rl.setVisibility(View.VISIBLE);
rl.post(new Runnable()
{
int dpToPx(final int dp)
{
return (int) (dp * getResources().getSystem().getDisplayMetrics().density + 0.5f);
}
#Override
public void run() {
int h = rl.getHeight(); //height is ready
h = dpToPx(h);
ImageButton button = (ImageButton)inflate.findViewById(R.id.button1);
float y = button.getY();
button.setY((float)h+y - 1100);
ImageButton button2 = (ImageButton)inflate.findViewById(R.id.button2);
y = button2.getY();
button2.setY((float)h+y);
}
});
The button with the -1100 (That number was just something I checked to see how it affects the position of the button and will not stay there obviously) is showing where I want it to be. The other button is so high or low which is no longer visible.
How do I set the position such that the button's Y position will be the old position + the height of the newly shown relative layout so the buttons will show just above it?
This is straightforward, all we need to do is to position the buttons at the y coordinate of our RelativeLayout.
We can get the y coordinate by calling:
rl.getY();
And since we want the button to be above the rl, we will subtract its height from the y coordinate of rl, something like this:
button.setY(rl.getY() - button.getHeight());
there's something I want to do but don't know how... that would be a button that slides from the bottom of the screen to a certain position, triggered by something. Very similar to how snackbar show, with the difference that it stays in place instead of disappearing and has the property of being "clickable". This would NOT be a panel draggable from the bottom, but a panel that slides automatically triggered by something.
How can I accomplish this?
Sounds like a fairly straightforward animation along the y axis, with a start value that matches the height of the screen (such that it renders just off screen), to whatever the final value is. Below code is from memory, but it should work.
To grab the screen height:
int getScreenHeight() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
}
And to animate a view (in this case, to 80% of the total screen height):
void animateOnScreen(View view) {
final int screenHeight = getScreenHeight();
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", screenHeight, (screenHeight * 0.8F));
animator.setInterpolator(new DecelerateInterpolator());
animator.start();
}
Sliding a View down by a distance:
view.animate().translationY(distance);
You can later slide the View back to its original position like this:
view.animate().translationY(0);
I needed to get constant aspect ratio of TextView with background image (9patch). So i used the code below in my activity:
#Override
public void onResume() {
super.onResume();
// adding contact image resize callback for adjusting image height
findViewById(R.id.contactText).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// aspect ratio
float resizeRatio = 1.13f;
// getting contact frame
TextView contactView = (TextView) findViewById(R.id.contactText);
ViewGroup.LayoutParams layout = contactView.getLayoutParams();
// resizing contact text
layout.height = (int) (contactView.getWidth() * resizeRatio);
contactView.setLayoutParams(layout);
contactView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
This code does what i expect - TextView is resized. But the borders (padding) for text, described in nine-patch are get from not resized image. So text is displayed in the center of TextView instead of bottom, where the padding borders are located.
How to fix this problem?
The problem is because of constant top padding of image even after stretch. When android stretches image vertically it keeps top padding value. So if padded area is not in stretched area it increases though it is not stretched at all.
This problem is described here
I have a LinearLayout (LayoutContentView) which can contain one or two view (SubView1 and SubView2) (they are eitherTextView or ImageView).
This layout is in another LinearLayout (MyScreenLayout).
I want to make an animation on LayoutContentView so it can move and show just a part of it in MyScreenLayout.
Both of these layouts have setClipChildren(false); so it can let it's children draw outside it-self.
Depending on different parameters, I can change the size of the content, and the size of the content I will show.
Basically, I expend from top to bottom to show the two subviews and unexpend for bottom to top to show only the second subview. Before I expend, I increase the size of the LayoutContentView, so it can show the two subviews, and after I unexpend, I decrease the size of the LayoutContentView, so it can only show the second subview, and it let space on the screen for other elements.
Here is my method for expending and un-expending LayoutContentView :
mLayoutContentView.clearAnimation();
float yFrom = 0.0F;
float yTo = 0.0F;
float xFrom = 0.0F;
float xTo = 0.0F;
if (expend) { // if we expend
// I change the height of my LayoutContentView so it we can show it's two subviews
final android.view.ViewGroup.LayoutParams lp = mLayoutContentView.getLayoutParams();
lp.height = subView1H + subView2H;
setLayoutParams(lp);
invalidate();
// we start the animation so it shows only the second subview
yFrom = -subView1H;
// and we animate from top to bottom until it shows the two subviews
yTo = 0;
} else { // if we un-expend
// we animate from bottom to top starting by showing the two subviews
yFrom = 0;
// and progressively hiding the first subview and showing only the second subview
yTo = -subView1H;
}
Animation anim = new TranslateAnimation(xFrom, xTo, yFrom, yTo);
anim.setDuration(1000);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (!expend) {
// if we un expend at the end of the animation we can set the size of LayoutContentView to the size of the second subview again
final android.view.ViewGroup.LayoutParams lp = mLayoutContentView.getLayoutParams();
lp.height = subView2H;
mLayoutContentView.setLayoutParams(lp);
invalidate();
}
}
});
mLayoutContentView.startAnimation(anim);
The way I made my animation I need it to apply on LayoutContentView, the layout which contain two subview, and with startAnimation() it doesn't do the animation.
I tried to use a LayoutAnimationController, but instead of doing the animation on the LayoutContentView, it does it on each of its children...
I also tried to do the animation on each children myself, but I don't know why, the second subview isn't shown.
Each time I've tried to use HierarchyViewer, but it's does see the change made by the animation.
Does anyone know a solution or have faced the same problem and found a good solution ?
EDIT :
Well it seems that if you set a background color to your TextView and move them with animation, the background move but even if you have set the fill after parameter to your animation, the background moves back to it's original position or something like that, and therefore as I set a background color to both of my SubViews, somewhat one of the SubView's background hide the background of the other...
And there also a problem if after the animation, one of the SubView is still outside the its layout, there is also a problem during the animation, so I add a limitation to what I intended to here too.