Created
December 10, 2015 20:59
-
-
Save akuafif/13fabb5c602a4dd1b0cc to your computer and use it in GitHub Desktop.
[Java/Android] VerticalTextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.example.app.VerticalTextView | |
android:id="@+id/textview_days" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Thursday" | |
android:layout_centerVertical="true" | |
android:layout_alignParentStart="true" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.widget.TextView; | |
public class VerticalTextView extends TextView { | |
final boolean topDown; | |
public VerticalTextView(Context context, AttributeSet attrs){ | |
super(context, attrs); | |
final int gravity = getGravity(); | |
if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { | |
setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP); | |
topDown = false; | |
}else | |
topDown = true; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ | |
super.onMeasure(heightMeasureSpec, widthMeasureSpec); | |
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); | |
} | |
@Override | |
protected boolean setFrame(int l, int t, int r, int b){ | |
return super.setFrame(l, t, l+(b-t), t+(r-l)); | |
} | |
@Override | |
public void draw(Canvas canvas){ | |
if(topDown){ | |
canvas.translate(getHeight(), 0); | |
canvas.rotate(90); | |
}else { | |
canvas.translate(0, getWidth()); | |
canvas.rotate(-90); | |
} | |
canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE); | |
super.draw(canvas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment