Android - button disappears when resized - android-view

I am trying to have a button which scales dynamically. At run time, I want its width and height to be 70% of the current size. However, the button is disappearing. Here is my code:
Button btn = (Button) v.findViewById(R.id.button_delete_transaction);
btn.setMinWidth(0);
btn.setMinHeight(0);
btn.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width = btn.getMeasuredWidth();
int height = btn.getMeasuredHeight();
ViewGroup.LayoutParams params = btn.getLayoutParams();
params.width = (int) .7 * width;
params.height = (int) .7 * height;
btn.setLayoutParams(params);
And the xml:
<Button
android:id="#+id/button_delete_transaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/add_img"
android:focusable="false"
/>

EDIT:
Ahh...it is because you are not casting the right thing as an int. You are casting 0.7 as an int (which goes to zero) and then multiplying it, instead of multiplying and then casting. You can use (int) (.7 * width) instead of (int) .7 * width.
See my example: http://ideone.com/NSGwGF
Anyway, my advice below still stands.
Why not use:
btn.setWidth((int) Math.round(.7 * width));
btn.setHeight((int) Math.round(.7 * height));
instead of:
ViewGroup.LayoutParams params = btn.getLayoutParams();
params.width = (int) .7 * width;
params.height = (int) .7 * height;
btn.setLayoutParams(params);

Related

Appium scrollto()/hidekeyboard() methods are not working with client jar 3.3.0/3.4.0 in iphone

I have tried most of the possible method(java script/touchaction) but not able to scroll
if any one have solution please help me, thanks
Try below code:
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int endy = (int) (size.height * 0.75);
int starty = (int) (size.height * 0.20);
driver.swipe(x, starty, x, endy, 1000);
For more details check below post:
Click Here

scroll functionality is not working in ipad, can anyone do help on this? using appium

I am trying to do scroll functionality ipad I tried all the possibility
ex: ScrollTo("") and ScrollToExtract() and also JavaScript am not able to scroll in ipad but iphone it's working fine
Use this method:
public void scrollDown() {
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int starty = (int) (size.height * 0.85);
int endy = (int) (size.height * 0.10);
driver.swipe(x, starty, x, endy, 2000);
}

How to make random height with some space between previous and next height in Objective-C?

I use this code in Objective-C to generate a random height between 100 and 1000.
My problem is, that new height is often near to the previous one and that is not so nice.
So how to make it so, that there is always some space (50px, for example) between previous and next height?
_randomHeight = arc4random() % (1000-100+1);
You just have to keep generating values until a value meets your requirement:
CGFloat height;
do {
height = arc4random() % (1000-100+1);
} while (fabs(_randomHeight - height) < 50.0f);
_randomHeight = height;
This will Give You correct height
float _randomHight = arc4random() % 900 + 101;
Here is a solution to produce values between 100 and 1000, distant of 50 at least, and using only one call to arc4random to guarantee a fixed execution time:
/// Return a new random height between 100 and 1000, at least 50 space from previous one.
+ (uint32_t)randomHeight {
/// initial value is given equiprobability (1000 - 100 + 50)
static uint32_t _randomHeight = 950;
uint32_t lowValues = MAX(_randomHeight - 50, 0);
uint32_t highValues = MAX(850 - _randomHeight, 0);
uint32_t aRandom = arc4random_uniform(lowValues + highValues + 1);
_randomHeight = aRandom < lowValues ? aRandom : _randomHeight + 50 + aRandom - lowValues;
return _randomHeight + 100;
}

Per Pixel collision when animate sprites

This is what I have for detecting collision.
public static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB)
{
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];
if (colorA.A != 0 && colorB.A != 0)
{
return true;
}
}
}
return false;
}:
It work fine until I want to animate stuff. So I have a texture sprite that have about 12 frame. what I need to do is get the color data array of each frame. This is how I get the color data array:
Color[] playerColorArray = new Color[playerColorArray.X * playerColorArray.Y];
PlayerTexture.GetData(playerColorArray);
CData = playerColorArray;
Now my guess is that i have to update the textureData everytime the frame changes
Is there a way to get the the color data from each frame only?
You can get an array of the complete sprite sheet texture and only use the current frame.
Let's say you have a sprite sheet and stride is the offset of a pixel to the pixel below it. This can be the sprite sheet's width. Furthermore, you have the position x0, y0 of the first pixel of the current frame. Then you just have to modify the index calculation:
int posXInFrame = (x - rectangleA.Left);
int posYInFrame = (y - rectangleA.Top);
Color colorA = dataA[(posXInFrame + x0) + (posYInFrame + y0) * stride];
Probably, you have calculated x0 and y0 somewhere else and can pass those values to the function.

How image pixel data "scans" the image pixels?

The Goal:
Finding the first black pixel on the left side of an image that contains black and transparent pixels only.
What I have:
I know how to get the pixel data and have an array of black and transparent pixels (found it here : https://stackoverflow.com/a/1262893/358480 ):
+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
for (int ii = 0 ; ii < count ; ++ii)
{
NSUInteger alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;
[result addObject:[NSNumber numberWithInt:alpha]];
}
free(rawData);
return result;
}
What is the problem ?
I can not understand the order which the function "scans" the image.
What i want is to get only the columns of the image and locate the first column that has at list 1 non-transperant pixel. this way I will know how to crop the left, transparent side of the image?
How can I get the pixels by columns?
Thanks
Shani
The bytes are ordered left-to-right, top-to-bottom. So to do what you want, I think you want to loop over the rawData like this:
int x = 0;
int y = 0;
BOOL found = NO;
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
unsigned char alphaByte = rawData[(y*bytesPerRow)+(x*bytesPerPixel)+3];
if (alphaByte > 0) {
found = YES;
break;
}
}
if (found) break;
}
NSLog(#"First non-transparent pixel at %i, %i", x, y);
Then your first column that contains a non-transparent pixel will be column x.
Normally one would iterate over the image array from top to bottom over rows, and within each row from left to right over the columns. In this case you want the reverse: we want to iterate over each column, beginning at the left, and within the column we go over all rows and check if a black pixel is present.
This will give you the left-most black pixel:
size_t maxIndex = height * bytesPerRow;
for (size_t x = 0; x < bytesPerRow; x += bytesPerPixel)
{
for (size_t index = x; index < maxIndex; index += bytesPerRow)
{
if (rawData[index + 3] > 0)
{
goto exitLoop;
}
}
}
exitLoop:
if (x < bytesPerRow)
{
x /= bytesPerPixel;
// left most column is `x`
}
Well, this is equal to mattjgalloway, just slightly optimized, and neater too :O
Although a goto is usually permitted to abandon two loops from within the inner loop, it's still ugly. Makes me really miss those nifty flow control statements D has...
The function you provided in the example code does something different though. It starts at a certain position in the image (defined by xx and yy), and goes over count pixels going from the starting position to the right, continuing to next rows. It adds those alpha values to some array I suspect.
When passed xx = yy = 0, this will find the top-most pixel with certain conditions, not the left-most. This transformation is given by the code above. Do remind that a 2D image is simply a 1D array in memory, starting with the top row from left to right and proceeding with the next rows. Doing simple math one can iterate over rows or over columns.

Resources