How to avoid stretching image in imageView - android-studio

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

Related

Unable to achieve -90 degrees rotated textView in linearLayout in Android Studio

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!

Processing- What could be possible errors of svg file not displayed?

I'm trying to display an svg image with Processing, but it would only show a blank white space. I don't think there is any problem with the code as it works perfectly when I change the shape to ellipse. Do I have to modify any code below to display the svg file, or is there any other possible reasons why it wouldn't show? Thanks in advance!
Table table;
PFont f;
PShape leaf;
color [] c = {color(225, 50, 50), color(225, 100, 0), color(225, 225, 0), color(0, 150, 0), color(0), color(125)};
int i=0;
void setup() {
size(1100, 500);
background(255);
table=loadTable("P3_data.csv", "header");
leaf= loadShape("leaf.svg");
leaf.disableStyle();
}
void draw() {
stroke(255);
strokeWeight(0.1);
for (TableRow row : table.rows()) {
int friend= (row.getInt("Friend"));
int travel= (row.getInt("Travel"));
int selfimprovement= (row.getInt("Self-improvement"));
int club= (row.getInt("Club"));
int schoolwork= (row.getInt("Schoolwork"));
int money= (row.getInt("Money"));
int total= 0;
int [] Daily= {friend, travel, selfimprovement, club, schoolwork, money};
for (int k=0; k<6; k++) {
total +=Daily[k];
}
println (total);
for (int j=0; j<6; j++) {
for (int m=0; m< Daily[j]; m++) {
fill(c[j]);
ellipse((i%120)*10+10, (i/120)*40+10, 3*total, 4*total);
//shape(leaf, (i%120)*10+10, (i/120)*40+10, 3*total, 3*total);
total --;
}
}
if (i>1095) {
break;
}
i++;
}
save("sketch.png");
}
See the documentation of disableStyle()
Shapes are loaded with style information that tells them how to draw (the color, stroke weight, etc.) The disableStyle() method of PShape turns off this information. The enableStyle() method turns it back on.
If you want to display the SVG with its style, the you have to remove the disableStyle call:
leaf = loadShape("leaf.svg");
leaf.disableStyle();
If you want to change the stroke and fill color of the shape generated form the *svg" file, then indeed you have to disable the style:
leaf= loadShape("leaf.svg");
leaf.disableStyle();
In this case the shape is drawn with the current fill and stroke color. This means that all the shape is filled with the same fill color and drawn with the same stroke color:
for (int m=0; m< Daily[j]; m++) {
stroke(0, 0, 255); // blue
fill(255, 0, 0); // red
shape(leaf, (i%120)*10+10, (i/120)*40+10, 3*total, 3*total);
total --;
}

Memory Leak happens in Xamarin Forms Android while using Bitmap

While using bitmap in Xamarin Forms Android I'm getting out of memory error while loading images which are of higher than the usual size.
void getImage() {
-------
imageView.setImageBitmap(Bitmap.createScaledBitmap(getBitmapFromViewUpdated(_view), (int) width, (int) height, true));
tempView = imageView;
--------
}
public static Bitmap getBitmapFromView(final View view) {
view.setLayoutParams(new LayoutParams(view.getWidth(),
view.getHeight()));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
final Bitmap bitmap_ = Bitmap
.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap_);
view.draw(c);
return bitmap_;
}
Bitmap getBitmapFromViewUpdated(final View view) {
if (view.getWidth() == 0 || view.getHeight() == 0)
view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
else
view.setLayoutParams(new LayoutParams(view.getWidth(),
view.getHeight()));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, viewWidth, viewHeight);
final Bitmap bitmap_;
if(viewWidth>0 && viewHeight>0) {
bitmap_ = Bitmap
.createBitmap(viewWidth,
viewHeight, Bitmap.Config.ARGB_8888);
}
else
{
bitmap_= Bitmap
.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
}
Canvas c = new Canvas(bitmap_);
view.draw(c);
return bitmap_;
}
I have tried android:largeHeap=true, can achieve the result while using that but I need to know how to fix with bitmap resizing.
You can check these two links for efficient bitmap management in xamarin android
https://forums.xamarin.com/discussion/5716/disposing-of-bitmaps-properly
https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/
GoodLuck!

How to get the height and width of my layout

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();
}
}

Crop area selection control(like photoshop's) in c# windows form

how to develop a crop selection control like photoshop's in c# 4.0 in widows form application.
I have a windows form application in c# 4.0 that can crop images. At first you have to draw a rectangle using mouse to select the cropped region.
private Point _pt;
private Point _pt2;
private void picBoxImageProcessing_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int ix = (int)(e.X / _zoom);
int iy = (int)(e.Y / _zoom);
//reset _pt2
_pt2 = new Point(0, 0);
_pt = new Point(ix, iy);
// pictureBox1.Invalidate();
picBoxImageProcessing.Invalidate();
}
}
private void picBoxImageProcessing_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _selecting)
{
_selecting = false;
}
}
private void picBoxImageProcessing_Paint(object sender, PaintEventArgs e)
{
if (_selecting &&_pt.X >= 0 && _pt.Y >= 0 && _pt2.X >= 0 && _pt2.Y >= 0)
{
e.Graphics.DrawRectangle(pen, _pt.X * _zoom, _pt.Y * _zoom,
(_pt2.X - _pt.X) * _zoom, (_pt2.Y - _pt.Y) * _zoom);
}
}
private void picBoxImageProcessing_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
int ix = (int)(e.X / _zoom);
int iy = (int)(e.Y / _zoom);
_pt2 = new Point(ix, iy);
// pictureBox1.Invalidate();
picBoxImageProcessing.Invalidate();
}
}
there is no problem to draw the rectangle by mouse dragging. But if i want to change the height or width of the rectangle then I have to draw a new rectangle, that i don't want. I want to change the height and width of the rectangle by modifying the drawn rectangle instead of drawing a new rectangle.
I don’t want to know how to crop. I need to draw a resizable rectangle on the image as we can do in photoshop.
So I need a crop selection control like photoshop's crop control.

Resources