Rotated ( 90 degrees ) root ViewGroup - android

I am trying to create ViewGroup based on FrameLayout that might be rotated 90 degrees CW / CCW and it still will be working correctly
So far my results are not so sucesful. So far it looks like that ( left side before rotation, right after; sorry for bright red )
Layout for Activity
<?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">
<com.example.TestProject.RotatedFrameLayout
android:id="#+id/container"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F"/>
</RelativeLayout>
RotatedFrameLayout
public class RotatedFrameLayout extends FrameLayout {
private boolean firstMeasure = true;
public RotatedFrameLayout( Context context ) {
super( context );
init();
}
public RotatedFrameLayout( Context context, AttributeSet attrs ) {
super( context, attrs );
init();
}
public RotatedFrameLayout( Context context, AttributeSet attrs, int defStyle ) {
super( context, attrs, defStyle );
init();
}
private void init() {
setRotation( 90f );
}
#Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
super.onMeasure( heightMeasureSpec, widthMeasureSpec );
}
}
Some extra info
I don't want to use Animation rotation because buttons aren't clickable that way
I don't want to use landscape mode because in landscape on screen navigation buttons took a lot of space on Nexus 7 ( this is the main reason why I am trying to greate that rotated
It seems that only left and right side of the screen are out of bounds

It is quite hard to do and I think it is not worth doing. But if you really want to do this you need:
pass to ViewGroup correct size dimentions (swap width and height).
rotate ViewGroup canvas 90 degrees.
At this point everything should look fine, but touch events not working properly.
intercept all touch events and swap x and y. Then pass fixed events to ViewGroup.
I dont have any code samples and have never seen any ) This way should work, we did scale transformations with fragments where we had to fix touch events coordinates when fragment was scaled.
I havent tested it heavily but this works:
public class RotatedFrameLayout extends FrameLayout {
public RotatedFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public RotatedFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RotatedFrameLayout(Context context) {
super(context);
init();
}
#SuppressLint("NewApi")
private void init() {
setPivotX(0);
setPivotY(0);
setRotation(90f);
}
#SuppressLint("NewApi")
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setTranslationX(getMeasuredHeight());
}
}

Related

setMinHeight and setMaxHeight in android custom view

Hi I want to set android custom buttons' setMinHeight and setMaxHeight
I have android button widget in the Library project and user of SDK(library project) can take use of that custom Button but I want to put restriction that button's Minimum size has to be 200dp and button's maximum height can not exceed 350dp how do i achieve that from custom view Button custom class?
Tried searching lot of thread but not sure yet.
public class MyCustomButton extends android.widget.Button {
public MyCustomButton(Context context) {
super(context);
applyDefaults(null);
}
public MyCustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
applyDefaults(attrs);
}
public MyCustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
applyDefaults(attrs);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyCustomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
applyDefaults(attrs);
}
private void applyDefaults(AttributeSet attrs) {
String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
String width = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_width");
Log.e("MVYAS=======", "height===" + height);
Log.e("MVYAS=======", "width===" + width);
setUpButtonForLoginOrLogout();
setAllCaps(false);
setMinHeight(getResources().getDimensionPixelSize(R.dimen.mid_button_min_height));
setMinWidth(getResources().getDimensionPixelSize(R.dimen.mid_button_min_width));
setMaxHeight(getResources().getDimensionPixelSize(R.dimen.mid_button_max_height));
setMaxWidth(getResources().getDimensionPixelSize(R.dimen.mid_button_max_width));
}
}
and while using into layout.xml file
<com.example.library.widget.MyCustomButton
android:id="#+id/my_button"
android:layout_width="153dp"
android:layout_height="57dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_margin="10dp"
styleButton:mobileid="inverted"/>
Here user should not be able to create button height to 57dp as its lower than the desired button height.
Putting restriction by adding setMinHeight() and setMaxHeight() dose not work.
How do i achieve that. Your help appreciated.
Thanks in Advance.
Just add these below two methods in MyCustomButtom class and try it:
#Override
public void setMinimumHeight(int minHeight) {
super.setMinimumHeight(minHeight);
}
#Override
public void setMinHeight(#Px int minHeight) {
super.setMinHeight(minHeight);
}
And one more thing, add below line in applyDefaults() function.
setMinimumHeight(getResources().getDimensionPixelSize(R.dimen.mid_button_min_height));

Android ImageButton has unequal width and height

I have an 32x32 image in an ImageButton. Although layout_width and layout_height are set to wrap_content the ImageButton is shown as a rectangle with the height greater than the width. The container layout is a LinearLayout, nothing special around weighting the size of other widgets in this layout.
Why is this? It seems after I have upgraded my phone from 4.1.1. to 4.4 this strange behavior came in.
Is the image fixed size. If yes means you can mention the fixed size instead of using wrap content.
Or if it's dynamic means you can use the below SquareImageview
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
Use this class in your app.
Then call this SquareImageView instead of ImageView and the layout_height should be match_parent

View ignores alpha value after View.VISIBLE

I have a button ,with alpha set to 0.5, and its visibility is gone in the layout.
<Button
android:id="#+id/Button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/black_color"
android:alpha="0.5"
android:visibility="gone"/>
At some point, I wish to make it visible ( Button.setVisibility(View.VISIBLE); ), but when I do - it's not half-transparent (0.5). It appears as if alpha is set to 1.
This problem is usually the result of having android:animateLayoutChanges="true" in the parent view. The reason is that the layout animation of setting visibility also changes the alpha of the view and overrides the change made by setAlpha.
To resolve this you can either remove android:animateLayoutChanges="true" from the parent or create a custom view to set the visibility in onVisibilityChanged like this:
public class AlphaView extends View {
private static final String TAG = AlphaView.class.getSimpleName();
public AlphaView(Context context) {
super(context);
}
public AlphaView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public AlphaView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public AlphaView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onVisibilityChanged(#NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE) {
setAlpha(0.5f);
}
}
}
I ran into this problem as well. It seems to be a bug in Android itself. My solution was to avoid setting visibility, and to adjust alpha only. My view has a visibility of 'visible' in the XML, and starts off with the XML alpha tag value set to 0.0. Then, when I want it to be visible, I adjust the alpha programmatically:
dimmerView.setAlpha(.15f);
I disappear it by setting the alpha again to zero. Theoretically, you might need to adjust various views position on the z-axis with bringToFront (and in the case of a button you might want to remove its listener when alpha is set to zero), but in my implementation it did not seem to be necessary.
After setting Button Gone to Visible, add Alpha of the Button
like :
buttonObject.setAlpha(.5f);

Extended LinearLayout not rotating

I'm trying to create a custom LinearLayout that can rotate all children in the layout by a set angle. That is, I want to rotate the entire canvas and the children, not the children individually.
I need to support API Level 10, so android:rotate which was introduced in API Level 11 will not help me here.
I've been looking around and think I need to do something along the lines of this code below, but with this code nothing gets rotated.
Any ideas on what I'm missing here?
public class RotationLayout extends LinearLayout {
public RotationLayout(Context context) {
super(context);
init();
}
public RotationLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RotationLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setWillNotDraw(false);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate(350,0,0);
super.onDraw(canvas);
canvas.restore();
}
}`
I tried your code with a few changes.
In the onCreate(Bundle) of an activity class, I initialized the RotationLayout:
RotationLayout rl = new RotationLayout(this);
ImageView iv = new ImageView(this);
iv.setImageDrawable(getResources().getDrawable(R.drawable.some_drawable));
rl.addView(iv);
setContentView(rl);
And the view was indeed rotated 350 degrees(or 10 degrees counter-clockwise) around (0,0). This was what I changed:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.rotate(350,0,0);
}
From notes on Canvas.save() (Link):
Saves the current matrix and clip onto a private stack. Subsequent
calls to translate,scale,rotate,skew,concat or clipRect,clipPath will
all operate as usual, but when the balancing call to restore() is
made, those calls will be forgotten, and the settings that existed
before the save() will be reinstated.

Get Eclipse to show custom component on graphical xml view

I have simply extended the ImageView so an image goes full width. Like so:..
public class BannerImageView extends ImageView {
public BannerImageView(Context context) {
super(context);
}
public BannerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BannerImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
In XML I have declared it as follows:
<com.whatever.next.BannerImageView
android:id="#+id/banner"
android:src="#+drawable/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
I receive the following errors:
main.xml: Unable to resolve drawable "com.android.layoutlib.bridge.ResourceValue#48537a0f" in attribute "src"
then I get the expected null pointer exceptions.
I am confused as I thought since I am not altering the default behaviour of the ImageView it would show in the graphical layout. I have read through the other similar questions and that confused me some more.
For the record the above code works fine on an actual device.
Any help is appreciated!
instead of
android:src="#+drawable/logo"
use
android:src="#drawable/logo"
No + for the src attribute

Categories

Resources