Slide In View Animation - android

I want a relative layout to slide into a screen.
At the moment I set the layout to be below a layout that takes up the whole screen
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ghostIv"
android:src="#drawable/results_ghost"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="#+id/ghostShadowIv"
android:visibility="gone"
android:src="#drawable/results_ghost_shadow"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ghostIv"/>
<TextView
android:id="#+id/noFilterTv"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textColor="#color/LightTextGrey"
android:visibility="gone"
android:text="#string/no_filters"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/ghostShadowIv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<com.allryder.android.ui.views.ScheduleView
android:visibility="invisible"
android:background="#color/White"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scheduleView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>
<View
android:id="#+id/shadowV"
android:visibility="gone"
android:layout_above="#+id/ratingRl"
android:layout_height="4dp"
android:layout_width="match_parent"
android:background="#drawable/bottom_shadow"/>
<RelativeLayout
android:id="#+id/ratingRl"
android:layout_below="#+id/scheduleView"
android:background="#color/White"
android:clickable="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:background="#drawable/time_view_selector"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/ratePromptTv"
android:layout_toLeftOf="#+id/closeRatingIv"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center|left"
android:text="#string/rate_prompt"/>
<ImageView
android:background="#drawable/time_view_selector"
android:padding="10dp"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ratePromptTv"
android:layout_alignBottom="#+id/ratePromptTv"
android:id="#+id/closeRatingIv"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/search_clear"/>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<fragment android:id="#+id/filters_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:name="com.allryder.android.ui.fragments.FiltersDrawerFragment"/>
Then make this call
ratingRl.animate().translationY(- ratingRl.getHeight());
But it doesn't slide into the screen. If the view is in the screen to start off with, the view does animate. What is the problem?

ratingRl.animate().translationY(- ratingRl.getHeight()).setDuration(300).start();

I have done similar task in my recent project.
background = (RelativeLayout) findViewById(R.id.slidingBackground);
positionOffScreen(background);
private void positionOffScreen(View v){
View myView = v;
int amountOffscreen = (int)(getScreenWidth());
boolean offscreen = true;
int xOffset = (offscreen) ? amountOffscreen : 0;
RelativeLayout.LayoutParams rlParams = (RelativeLayout.LayoutParams)myView.getLayoutParams();
rlParams.setMargins(-1*xOffset, 0, xOffset, 0);
myView.setLayoutParams(rlParams);
}
protected float getScreenWidth() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return (float) displaymetrics.widthPixels;
}
When you want to start animation call this method
private void startAnimation(int duration){
TranslateAnimation a = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,getScreenWidth(),Animation.ABSOLUTE,0,Animation.ABSOLUTE,0);
a.setDuration(duration);
a.setFillAfter(false);
background.startAnimation(a);
}
When you want to interrupt animation
background.clearAnimation();
Here is layout example. The sliding view has to be placed on RelativeLayout.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<RelativeLayout
android:id="#+id/slidingBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#64000000"
android:orientation="vertical" >
</RelativeLayout>
...
And this is the app link where I use this sliding view.

Related

How to take screenshot of a layout having a camera preview and text view?

i want to take screenshot of camera and map and address text view only, whenever i try to take screenshot its only saving map and text area but the camera area gets all white screen onlyHere is my xml layout , and i want to take screenshot of relative layout with id rl_photo, but after taking screenshot of layout its only taking the map and textview but not camera image , camera image gets all white screen .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#android:color/black"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Photo"
android:visibility="gone"
android:textStyle="bold"
android:id="#+id/tv_save"
android:background="#android:color/black"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:textColor="#ffffff"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_above="#+id/rl_action"
android:id="#+id/rl_photo"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="120dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_height="120dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="gravity"
android:gravity="right"
android:layout_toRightOf="#+id/map"
android:layout_alignParentBottom="true"
android:background="#66000000"
android:layout_margin="10dp"
android:padding="10dp"
android:textColor="#android:color/white"
android:layout_alignParentRight="true"
android:id="#+id/tv_latlong"
android:text="xbdjhgjrejgtrjbtbjr"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/rl_action"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/button_capture"
android:layout_width="50dp"
android:layout_margin="10dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:src="#drawable/record"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="30dp"
android:src="#drawable/flip"
android:id="#+id/img_flip"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_height="30dp"/>
<ImageView
android:layout_width="30dp"
android:src="#drawable/gallery"
android:id="#+id/img_gallery"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_height="30dp"/>
</RelativeLayout>
</RelativeLayout>
For screenshot
Add image view on above of FrameLayout
While capturing the ss set camera bitmap in image view and make Imageview visible one process is done to make image view invisible
For SS follow below Process
XML file
<?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"
android:background="#android:color/black">
<TextView
android:id="#+id/tv_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="#android:color/black"
android:text="Save Photo"
android:textColor="#ffffff"
android:textStyle="bold"
android:visibility="gone" />
<RelativeLayout
android:id="#+id/rl_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl_action">
<ImageView
android:id="#+id/ImageViewOFCamera"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"/>
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp" />
<TextView
android:id="#+id/tv_latlong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/map"
android:background="#66000000"
android:gravity="right"
android:padding="10dp"
android:text="xbdjhgjrejgtrjbtbjr"
android:textAlignment="gravity"
android:textColor="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="#drawable/record" />
<ImageView
android:id="#+id/img_flip"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="#drawable/flip" />
<ImageView
android:id="#+id/img_gallery"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/gallery" />
</RelativeLayout>
</RelativeLayout>
for ss capture in java File
private Bitmap bitmap;
bitmap = ScreenshotUtil.getInstance().takeScreenshotForView( rl_photo);
public class ScreenshotUtil {
private static ScreenshotUtil mInstance;
private ScreenshotUtil() {
}
public static ScreenshotUtil getInstance() {
if (mInstance == null) {
synchronized (ScreenshotUtil.class) {
if (mInstance == null) {
mInstance = new ScreenshotUtil();
}
}
}
return mInstance;
}
public Bitmap takeScreenshotForView(View view) {
view.measure( MeasureSpec.makeMeasureSpec( view.getWidth(), MeasureSpec.EXACTLY ),
MeasureSpec.makeMeasureSpec( view.getHeight(), MeasureSpec.EXACTLY ) );
view.layout( (int) view.getX(), (int) view.getY(),
(int) view.getX() + view.getMeasuredWidth(),
(int) view.getY() + view.getMeasuredHeight() );
view.setDrawingCacheEnabled( true );
view.buildDrawingCache( true );
Bitmap bitmap = Bitmap.createBitmap( view.getDrawingCache() );
view.setDrawingCacheEnabled( false );
return bitmap;
}
public Bitmap takeScreenshotForScreen(Activity activity) {
return takeScreenshotForView( activity.getWindow().getDecorView().getRootView() );
}
}

How to keep all items bound to the bottom of recycler view when item expands?

Is there any way to keep the other, non-expanded items located at the bottom. In the second picture, the items shift up to the top--this is not desired.
Here is the item that is shown in the pictures above.
<LinearLayout 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:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="10dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/item_life_event_id"
android:background="#color/colorAccent"
android:layout_gravity="bottom">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:orientation="horizontal"
android:id="#+id/massive_text_view"
android:visibility="gone">
<TextView
android:id="#+id/textViewEventDate"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:text="TextView" />
<ImageView
android:id="#+id/imageEventNotice"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:srcCompat="#tools:sample/avatars" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageViewEventIcon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/textViewEventTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="5"
android:text="TextView" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is the parent layout where the recycler view is contained.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="99">
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
/>
<Button
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:focusable="true"
android:orientation="horizontal"
tools:listitem="#layout/item" />
</LinearLayout>
Here is my onScroll listener that is responsible for making the massive text view visible or gone:
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int centerPos = layoutManager.findFirstCompletelyVisibleItemPosition();
View centerView = lifeTimelineRV.getLayoutManager().findViewByPosition(centerPos);
if (prevCenterPos != centerPos) {
// dehighlight the previously highlighted view
View prevView = lifeTimelineRV.getLayoutManager().findViewByPosition(prevCenterPos);
if (prevView != null) {
View previousView = prevView.findViewById(R.id.item_life_event_id);
previousView.setSelected(false);
previousView.findViewById(R.id.massive_text_view).setVisibility(View.GONE);
Log.d(TAG, "onScrolled: not focused" + prevCenterPos);
// run scale animation and make it bigger
// Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.scale_out_item);
// previousView.startAnimation(anim);
// anim.setFillAfter(true);
}
// highlight view in the middle
if (centerView != null) {
View currentView = centerView.findViewById(R.id.item_life_event_id);
currentView.setSelected(true);
Log.d(TAG, "onScrolled: focused" + centerPos);
currentView.findViewById(R.id.massive_text_view).setVisibility(View.VISIBLE);
// run scale animation and make it smaller
// Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.scale_in_item);
// centerView.startAnimation(anim);
// anim.setFillAfter(true);
}
prevCenterPos = centerPos;
}
}
Here is my desired output. Is something like this possible? The first picture on the left is what I want. The second is what currently happens.

android swapping two views using animation when click on imagebutton

I have seen the questions in the stack over flow, even I tried,
Android Translate Animation like Swapping Two Views
But nothing worked for my scenario, I want to swap swap_above linear layout and swap_below linear layouts when click on swap image button. If possible I would like to apply animation for swap imagebutton also, when the views are being swapped.
Thank you,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/from"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="25dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/swap_above"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/from_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:id="#+id/from_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/seek_thumb_pressed"
android:drawablePadding="5dp"
android:text="BANGALORE"
android:textSize="20dp" />
<TextView
android:id="#+id/from_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=" (BLR)"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:background="#FFF" />
<View
android:layout_width="60dp"
android:layout_height="0dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/to"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="25dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/swap_below"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/to_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:id="#+id/to_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/seek_thumb_normal"
android:drawablePadding="5dp"
android:text="Hyderabad"
android:textSize="20dp" />
<TextView
android:id="#+id/to_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=" (HYD)"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ImageButton
android:id="#+id/swap"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical|end"
android:background="?android:attr/selectableItemBackground"
android:src="#drawable/seek_thumb_disabled" />
</FrameLayout>
</LinearLayout>
First - set the below xml attributes for each target view's parent which may be blocking the animation :
android:clipChildren="false"
android:clipToPadding="false"
You need to set this attributes at 3 places in your case.
Read more about clipChildren & clipToPadding here -
http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:clipChildren
http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:clipToPadding
If your minSDK >=12
Use this for swap animation :
findViewById(R.id.swap).setOnClickListener(new View.OnClickListener() {
boolean isAnimating;
#Override
public void onClick(View v) {
if(isAnimating)
return;
isAnimating=true;
View v1 = findViewById(R.id.swap_above);
View v2 = findViewById(R.id.swap_below);
float x1, y1, x2, y2;
x1 = getRelativeX(v1);//Use v1.getX() if v1 & v2 have same parent
y1 = getRelativeY(v1);//Use v1.getY() if v1 & v2 have same parent
x2 = getRelativeX(v2);//Use v2.getX() if v1 & v2 have same parent
y2 = getRelativeY(v2);//Use v2.getY() if v1 & v2 have same parent
float x_displacement = (x2-x1);
float y_displacement = (y2-y1);
v1.animate().xBy(x_displacement).yBy(y_displacement);
v2.animate().xBy(-x_displacement).yBy(-y_displacement);
long anim_duration = v1.animate().getDuration();
//Wait till animation is over to set isAnimating to false
//take 10 ms as buffer time to ensure proper functioning
//If you remove this timer & isAnimating variable, the animation will function improperly when user rapidly clicks on swap button
new CountDownTimer(anim_duration + 10, anim_duration + 10) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
isAnimating=false;
}
}.start();
}
//returns x-pos relative to root layout
private float getRelativeX(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getX();
else
return myView.getX() + getRelativeX((View) myView.getParent());
}
//returns y-pos relative to root layout
private float getRelativeY(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getY();
else
return myView.getY() + getRelativeY((View) myView.getParent());
}
});
Read about View Property animator here -
http://developer.android.com/reference/android/view/ViewPropertyAnimator.html
http://developer.android.com/guide/topics/graphics/prop-animation.html
Also, you should try to minimize the number of views in your layout.
Read about layout optimization here -
http://developer.android.com/training/improving-layouts/optimizing-layout.html

buttons on frame layout

Hi I am developing an android video app. I have buttons on my frame layout. I need the buttons a little big. But they are getting shrinked as I get the preview on the screen.
Video.xml looks as below
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/videoview"
android:layout_width="720px"
android:layout_height="480px" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/mybutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:src="#drawable/record_video" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/flashoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="OFF"
android:textSize="8dp" />
<RadioButton
android:id="#+id/flashtorch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Torch"
android:textSize="8dp" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
and the oncreate is as below
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.videocapture);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
vwidth = metrics.widthPixels;
vheight = metrics.heightPixels;
Log.e("vwidth"," "+vwidth);
Log.e("vheight"," "+vheight);
//Get Camera for preview
myCamera = getCameraInstance();
if(myCamera == null)
{
Toast.makeText(VideoCapture.this,"Fail to get Camera",Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
final FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myCameraPreview.post(new Runnable()
{
#Override
public void run()
{
int width = vwidth;
int height = vheight;
LayoutParams lp = myCameraPreview.getLayoutParams();
lp.height = height;
lp.width = width - 80;
myCameraPreview.setLayoutParams(lp);
}
});
recVideo = (ImageView)findViewById(R.id.mybutton);
recVideo.setOnClickListener(myButtonOnClickListener);
flashOff = (RadioButton)findViewById(R.id.flashoff);
flashTorch = (RadioButton)findViewById(R.id.flashtorch);
}
How do I resolve this. Please Help. Thanks
Try to use this layout , you can change weight according to your requirement.Please change background colors .Hope it will help.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/videoview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#FFCCCC" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#99FF66"
android:orientation="vertical" >
<ImageView
android:id="#+id/mybutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:src="#drawable/ic_launcher" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/flashoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="OFF"
android:textSize="8dp" />
<RadioButton
android:id="#+id/flashtorch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Torch"
android:textSize="8dp" />
</RadioGroup>
</LinearLayout>
</LinearLayout>

Android Video not fitting the width in portrait of the videoview and not occupying full screen in landscape

Android video not fitting the width of the videoview when in portrait, how can i make the width of my video fit the width of my videoview when in portrait, and when i change orientation of the screen to landscape i would like the video to fill the whole screen like the youtube app does. when in portrait the video is to start from the top to the middle of the screen and and when in landscape to fill the whole screen. here is what i have tried, i have also add links of the complete screenshot
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<VideoView
android:id="#+id/videoview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="53" >
</VideoView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:background="#drawable/top_bar"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/settings"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/settings" />
</LinearLayout>
<LinearLayout
android:id="#+id/low_tab"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/low" />
</LinearLayout>
<LinearLayout
android:id="#+id/high_tab"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/high" />
</LinearLayout>
<LinearLayout
android:id="#+id/audio_tab"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/audio" />
</LinearLayout>
<LinearLayout
android:id="#+id/full_screen"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/full" />
</LinearLayout>
<LinearLayout
android:id="#+id/fresh"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/refresh" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:background="#drawable/comment_bar"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/liveblog_tab"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="#drawable/select"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/live_blog" />
</LinearLayout>
<LinearLayout
android:id="#+id/addcom_tab"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/select"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingTop="5dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/add_comment" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="33" >
<LinearLayout
android:id="#+id/liveblog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/browser"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
</LinearLayout>
<LinearLayout
android:id="#+id/comments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
You need to have two different layouts. One for the portrait and the one for the landscape.
Create two xml files with the same name and put them into folders "layout-land" for landscape and "layout-port" for portrait.
And Here you can take a look how to handle orientation changes.
This can be the layout to make video view full screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView android:id="#+id/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
</VideoView>
</RelativeLayout>
EDIT : This is How you can handle it in the method.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
setContentView(Your Landscape layout);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setContentView(Your portrait layout);
}
}
For linearlayout I'm not sure. But using relativelayout you can do this as below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="#+id/videoview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
</VideoView>
//your other xml content
....
....
....
</RelativeLayout>
And If you want the videoview to be filled in height too. then you need to do this code:
<VideoView
android:id="#+id/videoview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
</VideoView>
//your other xml content
....
....
....
</RelativeLayout>
Let me know if it's helped. Thanks.
private void fitStretchClick() {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(this, Uri.parse(arrayListVideos.get(mPosition).getSongPath()));
Bitmap bmp = retriever.getFrameAtTime();
float width = ((float) bmp.getWidth()) / ((float) bmp.getHeight());
int width2 = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
float f = (float) width2;
float f2 = (float) height;
float f3 = f / f2;
ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams();
ViewGroup.LayoutParams layoutParams2 = mVideoView.getLayoutParams();
if (changedNow) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
mVideoView.setLayoutParams(params);
Toast.makeText(this, "Stretch To Screen", Toast.LENGTH_SHORT).show();
changedNow = false;
return;
} else {
changedNow = true;
Toast.makeText(this, "Fit To Screen", Toast.LENGTH_SHORT).show();
if (width > f3) {
layoutParams.width = width2;
layoutParams.height = (int) (f / width);
mVideoView.setLayoutParams(layoutParams);
return;
}
layoutParams.width = (int) (width * f2);
layoutParams.height = height;
mVideoView.setLayoutParams(layoutParams);
}
}

Categories

Resources