I am using this class as item decoration in a horizontal RecyclerView (#2), that is also included in a vertical recyclerView(#1):
holder.recyclerView.addItemDecoration(new CirclePagerIndicatorDecoration());
CirclePagerIndicatorDecoration.java:
public class CirclePagerIndicatorDecoration extends RecyclerView.ItemDecoration {
private int colorActive = 0xDEE54242;
private int colorInactive = 0x33000000;
private static final float DP = Resources.getSystem().getDisplayMetrics().density;
private final int mIndicatorHeight = (int) (DP * 16);
private final float mIndicatorStrokeWidth = DP * 4;
private final float mIndicatorItemLength = DP * 4;
private final float mIndicatorItemPadding = DP * 8;
private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
private final Paint mPaint = new Paint();
public CirclePagerIndicatorDecoration() {
mPaint.setStrokeWidth(mIndicatorStrokeWidth);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
int itemCount = parent.getAdapter().getItemCount();
// center horizontally, calculate width and subtract half from center
float totalLength = mIndicatorItemLength * itemCount;
float paddingBetweenItems = Math.max(0, itemCount - 1) * mIndicatorItemPadding;
float indicatorTotalWidth = totalLength + paddingBetweenItems;
float indicatorStartX = (parent.getWidth() - indicatorTotalWidth) / 2F;
// center vertically in the allotted space
float indicatorPosY = parent.getHeight() - mIndicatorHeight * 2F;
drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount);
// find active page (which should be highlighted)
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
int activePosition = layoutManager.findFirstVisibleItemPosition();
if (activePosition == RecyclerView.NO_POSITION) {
return;
}
// find offset of active page (if the user is scrolling)
final View activeChild = layoutManager.findViewByPosition(activePosition);
int left = activeChild.getLeft();
int width = activeChild.getWidth();
int right = activeChild.getRight();
// on swipe the active item will be positioned from [-width, 0]
// interpolate offset for smooth animation
float progress = mInterpolator.getInterpolation(left * -1 / (float) width);
drawHighlights(c, indicatorStartX, indicatorPosY, activePosition, progress);
}
private void drawInactiveIndicators(Canvas c, float indicatorStartX, float indicatorPosY, int itemCount) {
mPaint.setColor(colorInactive);
// width of item indicator including padding
final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
float start = indicatorStartX;
for (int i = 0; i < itemCount; i++) {
c.drawCircle(start, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
start += itemWidth;
}
}
private void drawHighlights(Canvas c, float indicatorStartX, float indicatorPosY,
int highlightPosition, float progress) {
mPaint.setColor(colorActive);
// width of item indicator including padding
final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
if (progress == 0F) {
// no swipe, draw a normal indicator
float highlightStart = indicatorStartX + itemWidth * highlightPosition;
c.drawCircle(highlightStart, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
} else {
float highlightStart = indicatorStartX + itemWidth * highlightPosition;
// calculate partial highlight
float partialLength = mIndicatorItemLength * progress + mIndicatorItemPadding*progress;
c.drawCircle(highlightStart + partialLength, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
}
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.bottom = mIndicatorHeight;
}
}
There is an issue with this code.
When scrolling down recyclerView #1, the circle indicators from recyclerView #2 are not always shown at the same place in recyclerview #2 .
Example: Item 1 from recyclerView #1 and circle pager indicators from item 1 from recyclerView #2.
Example: Item 5 from recyclerView #1 and circle pager indicators from item 1 from recyclerView #2.
And a second question is how to change the position from the circle indicators to the top side of the recyclerView #2.
EDIT: RecyclerView #1 layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/colorWhite"
android:padding="10dp"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_account_circle_black_24dp"
card_view:civ_border_color="#FF000000"
card_view:civ_border_width="2dp" />
<TextView
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_toRightOf="#id/profile_image"
android:text="Sample"
android:textColor="#color/grey_700"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtUsername"
android:layout_alignStart="#id/txtUsername"
android:layout_toRightOf="#id/profile_image"
android:text="TextView"
android:textSize="10sp" />
<LinearLayout
android:id="#+id/layout_deportes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/txtUsername"
android:layout_marginLeft="20dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#id/txtUsername"
android:orientation="horizontal">
<ImageView
android:id="#+id/sp1"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp6"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#+id/layout_deportes"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp2"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp3"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp4"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp5"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp7"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp8"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
</LinearLayout>
<TextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_image"
android:layout_marginStart="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:minLines="3"
android:paddingRight="40dp"
android:text="Vivimos rodeados de objetos cotidianos que, a pesar de verlos cada día, todavía no tenemos muy claro para qué sirven. Es el caso del cuadrado con dos ranuras que tienen algunas mochilas, el bolsillo pequeño de los vaqueros, sus remaches de cobre e incluso las siglas 'YKK' que aparecen en tu cremallera. ¿Alguna vez te habías preguntado para qué sirven los agujeros laterales de algunas zapatillas? "
android:textAlignment="textStart"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/addUser"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/layout_deportes"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
card_view:srcCompat="#drawable/home_add_user" />
<ImageView
android:id="#+id/tres"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/addUser"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
card_view:srcCompat="#drawable/home_submenu" />
<ImageView
android:id="#+id/ivAmbassador"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="#id/profile_image"
android:layout_alignBottom="#id/profile_image"
android:layout_marginLeft="50dp"
card_view:srcCompat="#drawable/home_ambassador" />
<ImageView
android:id="#+id/spot_sport"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/txtDescription"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/tipo_spot"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/txtDescription"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/spot_sport"
android:fadeScrollbars="true"
card_view:srcCompat="#drawable/negocio" />
<ImageView
android:id="#+id/spot_map"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/txtDescription"
android:layout_alignBottom="#id/tipo_spot"
android:layout_marginLeft="9dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/tipo_spot"
android:fadeScrollbars="true"
android:foregroundGravity="bottom"
card_view:srcCompat="#drawable/home_pin_mapa" />
<TextView
android:id="#+id/spot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtDescription"
android:layout_marginTop="15dp"
android:layout_toLeftOf="#id/verified"
android:layout_toRightOf="#id/spot_map"
android:ellipsize="end"
android:maxLines="1"
android:minLines="1"
android:text="Skatepark Macba, Barcelona, Spain"
android:textSize="12sp" />
<ImageView
android:id="#+id/verified"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignBottom="#id/spot_map"
android:layout_marginLeft="24dp"
android:layout_marginEnd="5dp"
android:layout_toLeftOf="#id/txtVerified"
card_view:srcCompat="#drawable/home_ambassador" />
<TextView
android:id="#+id/txtVerified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/verified"
android:layout_alignParentEnd="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="-18dp"
android:layout_marginEnd="25dp"
android:text="Verified"
android:textAlignment="textEnd"
android:textSize="12sp" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_below="#id/spot_sport"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:visibility="gone" >
</com.daimajia.slider.library.SliderLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerMarcas"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#id/spot_sport" />
<ImageView
android:id="#+id/fire"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/recyclerMarcas"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
card_view:srcCompat="#drawable/home_fire_activado" />
<ImageView
android:id="#+id/comments"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/recyclerMarcas"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:layout_toRightOf="#id/fire"
card_view:srcCompat="#drawable/home_comentarios" />
<ImageView
android:id="#+id/fixpin"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/recyclerMarcas"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:layout_toRightOf="#id/comments"
card_view:srcCompat="#drawable/home_check" />
<TextView
android:id="#+id/txtEstadoAqui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recyclerMarcas"
android:layout_alignBottom="#id/fixpin"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/fixpin"
android:gravity="center_horizontal|center_vertical"
android:lines="2"
android:text="He estado aqui"
android:textSize="14sp" />
<TextView
android:id="#+id/txtComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recyclerMarcas"
android:layout_alignBottom="#id/fixpin"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="#id/fireresult"
android:layout_toRightOf="#id/txtEstadoAqui"
android:gravity="center_horizontal|center_vertical"
android:lines="2"
android:text="10 comments"
android:textAlignment="viewEnd"
android:textSize="14sp" />
<ImageView
android:id="#+id/fireresult"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="#id/recyclerMarcas"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginTop="-10dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="10dp"
card_view:srcCompat="#drawable/home_fire_activado" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
I have solved the issue just putting the circle indicator at the top of the recyclerView.
float indicatorPosY = 40;
Now its position doesn't change at any item.
Related
I have two view as the picture shows, the first I drew a circle and lines, the second I have plenty of buttons, I want to superimpose the two view so that each line points to a buttons
enter image description here "Canvas"
[enter image description here]2
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView (Context context) {
super (context) ;
}
#Override
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius ;
radius = 50 ;
Paint paint = new Paint () ;
Paint paint1 = new Paint () ;
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
canvas.drawCircle(x / 2, y / 2, radius, paint);
int margin = 500;
int margin1 = 300;
int top = 0 + margin;
int bottom = canvas.getHeight() - margin;
int left = 0 + margin1;
int right = canvas.getWidth() - margin1;
int centerX = x / 2;
int centerY = y / 2;
canvas.drawLine(centerX, top, centerX, bottom,paint1);
canvas.drawLine(left, centerY, right, centerY,paint1);
}}}
RelativeLayout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button1" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="#+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_alignParentEnd="true"
android:id="#+id/button3" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/button1"
android:id="#+id/button4" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="49dp"
android:id="#+id/button5" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_alignStart="#+id/button3"
android:id="#+id/button6" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignParentStart="true"
android:layout_marginBottom="58dp"
android:id="#+id/button7" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button7"
android:layout_alignParentEnd="true"
android:id="#+id/button8" />
</RelativeLayout>
Won't this work? It'll add 4 buttons at the centers horizontally and vertically at the ends of the lines that you've drawn.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button1" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="#+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:id="#+id/button3" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/button4" />
</RelativeLayout>
trying to make showing LinarLayout from GONE state to VISIBLE with "roll down" animation. On this Layout exist TextView. Seems to all work fine but, a TextView not shown after animation.
What I do wrong?
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/ll_info">
<TextView
android:id="#+id/bayer_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/buyer_note"/>
<RelativeLayout
android:id="#+id/button_continue"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:background="#drawable/button_register"
android:drawableEnd="#drawable/ic_arrow_forward_white_24dp">
<TextView
android:id="#+id/btn_cont_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Войти"
android:textColor="#android:color/white"
android:textSize="20sp"
android:fontFamily="fonts/SF-UI-Display-Regular.ttf"
android:layout_centerInParent="true" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:layout_alignParentEnd="false"
android:src="#drawable/ic_door"
android:layout_toRightOf="#+id/btn_cont_caption"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter" />
</RelativeLayout>
</LinearLayout>
And animation code:
final LinearLayout ll_info = (LinearLayout)findViewById(R.id.ll_info);
class scaleAnimation extends Animation {
public LinearLayout ll;
public int newHeight;
public void scaleTopHeight(int height)
{
newHeight = height;
}
public void setLayout(LinearLayout layout) {
ll = layout;
}
}
final LinearLayout ll_info = (LinearLayout)findViewById(R.id.ll_info);
scaleAnimation h = new scaleAnimation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
ll.getLayoutParams().height = interpolatedTime == 1
? LinearLayout.LayoutParams.WRAP_CONTENT
: (int)(newHeight * interpolatedTime);
ll.requestLayout();
}
};
h.setDuration(300);
h.setLayout(ll_info);
ll_info.setVisibility(View.VISIBLE);
ll_info.measure(View.MeasureSpec.makeMeasureSpec(((LinearLayout)ll_info.getParent()).getWidth(), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(((LinearLayout)ll_info.getParent()).getHeight(), View.MeasureSpec.AT_MOST));
h.scaleTopHeight(ll_info.getMeasuredHeight());
ll_info.getLayoutParams().height=1;
ll_info.startAnimation(h);
<RelativeLayout
android:id="#+id/button_continue"
android:layout_width="match_parent"
android:layout_height="54dp"
**android:layout_centerHorizontal="true"** /////
remove this
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:background="#drawable/button_register"
android:drawableEnd="#drawable/ic_arrow_forward_white_24dp">
<TextView
android:id="#+id/btn_cont_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Войти"
android:textColor="#android:color/white"
android:textSize="20sp"
android:fontFamily="fonts/SF-UI-Display-Regular.ttf"
android:layout_centerInParent="true" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:layout_alignParentEnd="false"
android:src="#drawable/ic_door"
android:layout_toRightOf="#+id/btn_cont_caption"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter" />
</RelativeLayout>
i think this is your xml file issue ,your relativelayout in center_horizontal and all the childs are in center in parent.so textview hides over imageview. Try it and let me know.
I have an image that is taking up all of the screen. I have tried increasing and decreasing the controls weight, adjust min and max heights and widths but i cant get the image height the same as the other controls.
<TextView
android:id="#+id/header_screenName_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/appBodyDefault"
android:textSize="#dimen/titleFontSize"
android:textColor="#color/white"
android:gravity="left"
android:text="#string/activity_newuser_header_string" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:background="#color/yellow"
android:layout_margin="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/firstName_editText"
android:layout_weight="1"
android:hint="First Name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/lastName_editText"
android:layout_weight="1"
android:hint="Last Name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/passwordOne_editText"
android:layout_weight="1"
android:hint="Password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordTwo_editText"
android:layout_weight="1"
android:hint="Re-type Password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/email_editText"
android:layout_weight="1"
android:hint="Email" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/btn_profile"
android:clickable="false"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
In android scale the image in a good way is hard, I'm using this method for that the image use the full space in the view without lose resolution.
(is only use this in the ImageView)
public static void ajustarImageViewToImage(final ImageView iv) {
ViewTreeObserver observer = iv.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Fazendo o redimensionamento das imagens para de acordo com o VIEW
Drawable drawable = iv.getDrawable();
int ih = drawable.getIntrinsicHeight();
int iw = drawable.getIntrinsicWidth();
int x = ((iv.getWidth() * ih) / iw) + 1;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, x);
lp.weight = 1;
lp.setMargins(0, 0, 0, 10);
iv.setLayoutParams(lp);
}
});
}
if you don't care about the resolution and the scale, try use android:scaleType="fitXY" in your ImageView on the XML.
I've found this animation code on SO. Everything works fine except of that animation is not fluid. In the previous application it was but in current - no. So there's actually issue with animation or with my layout. Here's animation code:
package de.silkcodeapps.lookup.utils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class AnimationUtils {
/**
* Expand the view.
*
* #param v
*/
public static void expand(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT
: (int) (targtetHeight * interpolatedTime);
v.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (targtetHeight / v.getContext().getResources()
.getDisplayMetrics().density));
v.startAnimation(a);
}
/**
* Collapse the view.
*
* #param v
* View to collapse.
*/
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight
- (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources()
.getDisplayMetrics().density));
v.startAnimation(a);
}
}
Here's my DialogFragment's layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/search_title"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textSize="#dimen/text_size_huge" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="#dimen/margin"
android:layout_marginTop="#dimen/margin"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="#+id/fragment_search_search_input"
style="#style/AccScreenEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionSearch" />
<de.silkcodeapps.lookup.ui.view.CheckableImageView
android:id="#+id/fragment_search_expand_collapse_additional_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:padding="#dimen/margin_small"
android:src="#drawable/btn_expand_collapse" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_search_additional_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/fragment_search_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/padding_very_small"
android:text="#string/search_help"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/fragment_search_and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/padding_very_small"
android:text="#string/search_and"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/padding_very_small"
android:text="#string/search_or"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_not"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/padding_very_small"
android:text="#string/search_not"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_single_placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_single_placeholder"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_multiple_placeholders"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_multiple_placeholders"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_exact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_exact"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_group"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_fuzzy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_fuzzy"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_must_contain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_must_contain"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
<TextView
android:id="#+id/fragment_search_relevance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="#dimen/padding_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_very_small"
android:text="#string/search_relevance"
android:textColor="#color/search_actions_text"
android:textSize="#dimen/text_size_huge" />
</LinearLayout>
<ProgressBar
android:id="#+id/fragment_search_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="#dimen/margin" />
<RadioGroup
android:id="#+id/fragment_search_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/margin"
android:background="#drawable/bg_black_normal"
android:orientation="horizontal"
android:padding="0dp" >
<RadioButton
android:id="#+id/fragment_search_in_version"
style="#style/ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/search_work" />
<RadioButton
android:id="#+id/fragment_search_on_desk"
style="#style/ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search_desk" />
</RadioGroup>
<TextView
android:id="#+id/fragment_search_results_overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/margin"
android:textSize="#dimen/text_size_huge"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/fragment_search_display_recent_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search_last_queries"
android:textSize="#dimen/text_size_huge"
android:textStyle="bold" />
<de.silkcodeapps.lookup.ui.view.CheckableImageView
android:id="#+id/fragment_search_expand_collapse_last_queries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:padding="#dimen/margin_small"
android:src="#drawable/btn_expand_collapse" />
</LinearLayout>
<ListView
android:id="#+id/fragment_search_last_queries"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin"
android:layout_weight="1"
android:footerDividersEnabled="true"
android:overScrollMode="never" />
<ListView
android:id="#+id/fragment_search_results"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin"
android:layout_weight="1"
android:footerDividersEnabled="true"
android:overScrollMode="never" />
<LinearLayout
android:id="#+id/fragment_search_external_search_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/fragment_search_google"
style="#style/OrangeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/margin_small"
android:drawableLeft="#drawable/btn_google"
android:text="#string/search_google" />
<TextView
android:id="#+id/fragment_search_wikipedia"
style="#style/OrangeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/btn_wikipedia"
android:text="#string/search_wikipedia" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I'm trying to animate fragment_search_additional_actions view.
Also while animation played I see that my view's content not cropped but shrinked down it's height while I want it to be cropped.
In applyTransformation you are calling v.requestLayout(); which is a complex operation (it has to remeasure all your views and redo the layout).
Maybe before it was fluid because your layout was simpler.
i've been scrounging the internet for a while now and have been unable to find s viable solution for my animation problem.
I have a list view where when you click on one of the items, more information animates from the bottom to give you a few lines of additional information. That is simply a linearlayout that i have inside the XML file im using for these list items, here:
<?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="wrap_content" >
<LinearLayout
android:id="#+id/friendActivityList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/friendInfo"
android:background="#color/grey"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:id="#+id/RelativeLayout04"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/ImageView04"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView04"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView03"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView02"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/imageView1"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView01"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/friendInfo"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="#drawable/bg_list_item_n" >
<ImageView android:id="#+id/imgCompany"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:layout_width="60dp"
android:src="#drawable/ic_launcher"
android:scaleType="centerInside"
android:layout_alignParentLeft="true"
android:layout_height="50dp">
</ImageView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_toRightOf="#id/imgCompany"
android:background="#android:color/transparent"
android:gravity="left|center"
android:orientation="vertical"
android:paddingLeft="5dp" >
<TextView android:id="#+id/lblCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="13dp"
android:text="Company Name">
</TextView>
<TextView android:id="#+id/lblReawrdDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:text="Reawrd Description">
</TextView>
<TextView android:id="#+id/lblScores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:singleLine="true"
android:text="My Score: 13434 | Top Score: 344425">
</TextView>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Most of this is just place holder information so that i can get the animation to work correctly. Here is the code that im using for the animation:
listviewFriends.setOnItemClickListener(new OnItemClickListener() {//this is the listView that im animating inside of
public void onItemClick(AdapterView<?> parent, View view,final int pos, long id) {
Log.v("ListItemClicked", "this position was clicked: "+pos);
if(friendInfoList != null){
friendInfoList.clearAnimation();//this is the new view that gets animated and is supposed to push everything below it out of the way
friendInfoList.setVisibility(View.GONE);
}
friendInfoList = (LinearLayout) view.findViewById(R.id.friendActivityList);
friendInfoList.setVisibility(View.VISIBLE);
friendInfoList.startAnimation(infoAnim);
}
});
infoAnim = new TranslateAnimation(0,0, -150, 0);//this is the animation im using
infoAnim.setDuration(1000);
infoAnim.setFillAfter(true);
No the result of this is that when i click on the list view item the entire space that the supposed-to-be-animated view takes up at the end is white while the view animates from the top down. The location is correct, but i want it to animate and push everything below it out of the way, instead it instantly pushes everything out of the way then animates to fill that space.
Any idea how i can get it to push everything out of the way during the animation instead of immediately? Is it even possible to achieve this effect? any help is greatly appreciated.
Also, I know how i can make it simply animate ontop of the other views, but i need it to actually push everything out of the way.
The trick with this is to create your own Animation subclass.
public class ExpandAnimation extends Animation
{
private int _targetHeight;
private View _view;
private boolean _down;
public ExpandAnimation(View view, int targetHeight)
{
_view = view;
_targetHeight = targetHeight;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
int newHeight;
if(_down)
{
newHeight = (int) (_targetHeight * interpolatedTime);
}
else
{
newHeight = (int) (_targetHeight * (1 - interpolatedTime));
}
_view.getLayoutParams().height = newHeight;
_view.requestLayout();
}
public ExpandAnimation expand()
{
_down = true;
return this;
}
public ExpandAnimation collapse()
{
_down = false;
return this;
}
#Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
}
#Override
public boolean willChangeBounds()
{
return true;
}
}
Then you can apply that to your view that should expand:
public void togglePreview()
{
if(_expanded) _preview.startAnimation(_animation.collapse());
else _preview.startAnimation(_animation.expand());
_expanded = !_expanded;
getParent().requestLayout();
}