I try in my Android App to create a thin -90 degrees rotated TextView (one horizontal text line for example 30dp width with 400 dp height). Unfortunately instead that the TextView takes the height and width of its parent (LinearLayout) in the LinearLayout there is a very small square (with no text cause it's too small) displayed.
XML
<LinearLayout
android:layout_width="30dp"
android:layout_height="match_parent"
android:background="#B33C3C3C"
android:id="#+id/verticalLayout"
android:orientation="vertical"
android:gravity="center">
</LinearLayout>
Kotlin
var width:Int?=null
var height:Int?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity)
var context: Context = this
val vto: ViewTreeObserver = verticalLayout.getViewTreeObserver()
vto.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
verticalLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this)
} else {
verticalLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this)
}
width = verticalLayout.measuredHeight
height = verticalLayout.measuredWidth
val t = TextView(context)
t.textSize = 15f
t.text = "Testttttttttttttttttttt"
t.rotation = -90f
t.setBackgroundResource(R.color.primary)
t.height = convertPixelsToDp(convertPixelsToDp(height!!, context), context)
t.width = convertPixelsToDp(convertPixelsToDp(width!!, context), context)
Log.d("aaa", "height " + height + " width " + width)
Log.d("aaa", "height " + convertPixelsToDp(height!!, context) + " width " + convertPixelsToDp(width!!, context))
verticalLayout.addView(t)
}
})
}
fun convertPixelsToDp(px: Int, context: Context): Int {
return px / (context.resources
.displayMetrics.densityDpi.toInt() / DisplayMetrics.DENSITY_DEFAULT)
}
The Log.d displays actually the correct numbers:
height 90 width 1415
height 30 width 471.
Do you have any ideas how I could achieve the desired thin rotated Text? Thank you!
Related
I am trying to display an image in imageView in landscape mode. when I select image from list, this image becomes stretched. This my code in xml
<com.project.widget.PaintView
android:id="#+id/paint_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
this is my code java to select image
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(bitmap == null){
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), Common.PICTURE_SELECTED);
bitmap= Bitmap.createScaledBitmap(srcBitmap, w,h,false);
for(int i=0;i<bitmap.getWidth();i++){
for(int j=0; j<bitmap.getHeight();j++){
int alpha = 255 - brightness(bitmap.getPixel(i,j));
if(alpha< 200){
bitmap.setPixel(i,j,Color.WHITE);
}else {
bitmap.setPixel(i, j, Color.BLACK);
}
}
}
}
Thanks for helping
this is the result i got image is too stretched
enter image description here
I have an imageview and I draw circles when touch on the screen, for selected points. But I didn’t want put the circles outside the imageview limits. The image is a SVG with the contours of the human body. How can I define the limits of image?
Here is my code:
Kotlin:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var layout: RelativeLayout = findViewById(R.id.layout1)
layout.addView(CustomView(this))
var imageBody: ImageView = findViewById(R.id.body)
var mBitmap: Bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
}
class CustomView(context: Context) : View(context) {
val brush = Paint()
var coordX: Float = 0.0f
var coordY: Float = 0.0f
override fun onTouchEvent(event: MotionEvent?): Boolean {
var X = event!!.getX()
var Y = event.getY()
coordX = X
coordY = Y
invalidate()
var text :String = "X: " + coordX + "\nY: " + coordY
Toast.makeText(context, text, Toast.LENGTH_LONG).show()
return super.onTouchEvent(event)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
/*Circle Color*/
brush.setARGB(255, 105, 103, 103)
/*Circle*/
canvas.drawCircle(coordX, coordY, 10f, brush)
}
}
Well, that's simple Math. In your onTouchEvent() implementation query getMeasuredWidth() and getMeasuredHeight() and clamp the coordinates of your touch event according to the circle you want to draw.
Since your circle has a radius of 10f, coordX is Math.max(10f, Math.min(X, getMeasuredWidth() - 10f)) and coordY likewise Math.max(10f, Math.min(X, getMeasuredHeight() - 10f)).
Im new in programming. the codes below is getting the screen size of the device .Here is my question. how can i replace this codes to get the width and height of my relativelayout?
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
This should get you going
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.layout_file);
int width = relLayout.getWidth();
int height = relLayout.getHeight();
Disclaimer: I did not get a chance to test this code.
Try this code. It works for me.
RelativeLayout = relativeLayout = findViewById(R.id.id_of_relativeLayout);
relativeLayout.post(new Runnable() {
#Override
public void run() {
int width = relativeLayout.getWidth();
int height = relativeLayout.getHeight();
}
}
What I have tried is
CAShapeLayer shapeLayer = new CAShapeLayer();
shapeLayer.Path = new CGPath();
shapeLayer.Path.AddLines(new CGPoint [] { startingPoint, endingPoint});
shapeLayer.StrokeColor = UIColor.Blue.CGColor;
shapeLayer.LineWidth = (System.nfloat)3.0;
shapeLayer.FillColor = UIColor.Clear.CGColor;
var width = Math.Abs(latestPoint.X - initioalPoint.X) + initioalPoint.X;
var height = Math.Abs(latestPoint.Y - initioalPoint.Y) + initioalPoint.Y;
var padding = 10;
shapeLayer.Frame = new CGRect(5, 5, width + padding - 2, height + padding - 2);
var view = new UIView();
view.Layer.AddSubLayer(shapeLayer);
CanvasView canvasDrawLine = new CanvasView(subView);
canvasDrawLine.Frame = new CGRect(5, 5, width + padding, height + padding[![enter image description here][1]][1]);
Public Class CanvasView : UIView
{
public CanvasView(drawline)
{
drawLine.ClipsToBounds = true;
var width = Math.Abs(endingPoint.X - initioalPoint.X) + initioalPoint.X;
var height = Math.Abs(endingPoint.Y - initioalPoint.Y) + initioalPoint.Y;
var padding = 10;
drawLine.Frame = new CGRect(5,5, width + padding, height + padding);
this.AddSubview(drawLine);
setUpGestures();
}
}
scrollView.AddSubview(canvasDrawLine );
when I set the width and height of the frame default, my gestures not working properly, for example If I have apply a pan gesture to the draw line even out of the draw line also pan gesture is working.How can I restrict all gestures are working only draw line area.
you can calculate bounds of your area covered by line :
CoreGraphics.CGPoint initialPoint = new CoreGraphics.CGPoint(1, 2);
CoreGraphics.CGPoint latestPoint = new CoreGraphics.CGPoint(1, 2);
var width = Math.Abs(latestPoint.X - initialPoint.X);
var height = Math.Abs(latestPoint.X - initialPoint.X);
var padding = 10;
subView.Frame = new CGRect(0, 0, width+padding, height+padding);
I have created sample code with simple view controller class -- Have assigned red colour to view containing line. And its working perfect. gesture recogniser is working on red part only --where line is.
public partial class ViewController : UIViewController
{
protected ViewController(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
CGPoint initioalPoint = new CGPoint(12, 12);
CGPoint latestPoint = new CGPoint(80, 45);
var shapeLayer = new CAShapeLayer();
shapeLayer.Path = new CGPath();
shapeLayer.Path.AddLines(new CGPoint[] { initioalPoint, latestPoint });
shapeLayer.StrokeColor = UIColor.Blue.CGColor;
shapeLayer.LineWidth = (System.nfloat)3.0;
shapeLayer.FillColor = UIColor.Clear.CGColor;
var subView = new UIView();
var width = Math.Abs(latestPoint.X - initioalPoint.X) + initioalPoint.X;
var height = Math.Abs(latestPoint.Y - initioalPoint.Y) + initioalPoint.Y;
var padding = 10;
subView.Frame = new CGRect(5, 5, width + padding, height + padding);
subView.Layer.AddSublayer(shapeLayer);
shapeLayer.Frame = new CGRect(5, 5, width + padding-2, height + padding-2);
subView.BackgroundColor = UIColor.Red;
var mainView = new UIView();
mainView.Frame = new CGRect(40, 40, width + padding+200, height + padding+200);
mainView.AddSubview(subView);
mainView.BackgroundColor = UIColor.Yellow;
this.View.AddSubview(mainView);
UITapGestureRecognizer tap = new UITapGestureRecognizer();
tap.AddTarget((obj) => {new UIAlertView("tapped","",null,"ok",null).Show();});
subView.AddGestureRecognizer(tap);
//UITapGestureRecognizer tap = new UITapGestureRecognizer(() => Console.WriteLine("tap"));
// Perform any additional setup after loading the view, typically from a nib.
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
///To check if Touch Point is inside Given Polygon::
CGPoint pt1 = new CGPoint(initioalPoint.X - 50, initioalPoint.Y - 50);
CGPoint pt2 = new CGPoint(initioalPoint.X + 50, initioalPoint.Y - 50);
CGPoint pt3 = new CGPoint(latestPoint.X - 50, latestPoint.Y + 50);
CGPoint pt4 = new CGPoint(latestPoint.X + 50, latestPoint.Y + 50);
UIBezierPath path = new UIBezierPath();
path.MoveTo(pt1);
path.AddLineTo(pt2);
path.AddLineTo(pt3);
path.AddLineTo(pt4);
path.AddLineTo(pt1);
if (!path.ContainsPoint(ToucPoint))
return;
Calling module:
private void Indicator_Move(object sender, EventArgs e)
{
Sizer size = new Sizer();
int x = this.Location.X;
int y = this.Location.Y;
int width = this.Width;
int height = this.Height;
var screenWidth = Screen.PrimaryScreen.WorkingArea.Width - 130;
if (x >= screenWidth)
{
size.checkmove(x, y, width, height, this);
}
else if (width == 158 )
{
width = 235;
height = 223;
this.Size = new System.Drawing.Size(width, height);
}
}
Module to Dock and reduce size from Sizer class:
public void checkmove(int movex, int movey,int width, int height, Form mover)
{
var moverheight = mover.Height;
var screenWidth = Screen.PrimaryScreen.WorkingArea.Width - 130;
var screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
var finalx = screenWidth;
if (movex > screenWidth)
{
mover.Size = new System.Drawing.Size(154, 45);
mover.Location = new Point(finalx, screenHeight - 600);
}
}
I actually found a different way to accomplish what I wanted by the double_click event on the form and just moving it out on to the desktop it full size, but I am still curious why it resizes back and forth to full size and reduced size while moving and appearing to flicker and ending up reduced and only showing full size when the mouse button is clicked on form and held down. I'm thinking it has to do with the resizeend call anytime the form is moved. And thanks Joran for the edit.