I want to get RANDOM value in my function, But n always equals 0. What am I doing wrong?
image = (ImageView) findViewById(R.id.imageView1);
//блок Random
Log.d(Tag, "1");
int NUM_IMAGES=7;
int imageArr[] = new int[NUM_IMAGES];
Log.d(Tag, "2");
imageArr[0] = R.drawable.image1;
imageArr[1] = R.drawable.image2;
imageArr[2] = R.drawable.image3;
imageArr[3] = R.drawable.image4;
imageArr[4] = R.drawable.image5;
imageArr[5] = R.drawable.image6;
imageArr[6] = R.drawable.image7;
Log.d(Tag, "3");
int n = (int)Math.random()*NUM_IMAGES;
Log.d(Tag, "n="+n);
image.setImageResource(imageArr[n]);
(int) Math.random() is executed first and will always return 0. Then multiplying NUM_IMAGES by 0 still returns 0.
You should add brackets around your expression so the conversion to int will be done after the multiplication :
int n = (int) (Math.random()*NUM_IMAGES);
If you read here, the priority of type cast is more than that of multiplication. Also the priority of ()(bracket) is higher than that of type cast. So the right way might be to put multiplication in bracket so that multiplication happens before casting. Something like:
int n = (int)(Math.random() * NUM_IMAGES);
Notice the brackets. Hope this helps.
Related
I have a Card widget with TextFormField as a child. I want to increase the height of the card each time a user gets to a new line. The card is wrapped in a Container and I have a height variable to determine the height.
How can I detect new line in TextFormField? Also whenever the text wraps to the next line.
I think you need to create a function to check the TextFormField specific line.
I think this an be useful for you "https://stackoverflow.com/questions/45900387/multi-line-textfield-in-flutter"
To update height without pressing enter:
onChanged: (String e) {
int sizeIncreaseConstant = 30; //the fontSize 20 + buffer
int widthOfCharacter = 17; // 85% of fontsize
int newNumLines = ((e.length * widthOfCharacter)/widthOfContainer).truncate();
if( newNumLines != numLines) {
setState(() {
if(newNumLines > numLines)
heightOfContainer = heightOfContainer + sizeIncreaseConstant;
else
heightOfContainer = heightOfContainer - sizeIncreaseConstant;
numLines = newNumLines;
});
}
},
initial values:
int numLines = 0;
double widthOfContainer = 120;
double heightOfContainer = 50;
//fontSize = 20;
For this, you will have to use a font that has equal width for all characters like Monospace. And you will also need to determine the width of the character based on fontSize. It is supposed to be 50-60% of the font size but 85% worked for me.
I am trying to convert a decimal into a character.
I have my alphabet which has been converted to a charArray
String alphabet = "abcdefghijklmnopqrstuvwxyz";
alpha = alphabet.toCharArray();
I was using binary numbers so values only from 0 to 255, however when I try to execute this code, it does not work.
private int toChar(int encryptCode){
int base = 26;
int characterSteps = (encryptCode/255)*base;
char character = alpha[characterSteps];
return character;
Lets say I have the decimal 78, 78/255 * 26 would give 7.95 (being int rounds to 8)
It should look up the alpha array and give 'h'. But every character gives 'a' meaning that (encryptCode/255)*base isn't working as intended.
Kevin is right in the comments. Try rearranging your formula like this:
int characterSteps = (encryptCode*base)/255;
change
int characterSteps = (encryptCode/255)*base;
to
int characterSteps = (int) (encryptCode/255.0)*base;
and you are done! 255.0 will cause broadening on encryptCode and base, and then cast to int.
Lets say I have the decimal 78, 78/255 * 26 would give 7.95 (being int rounds to 8)
Casting float/double to int will result in truncation and not round off. 7.95 will be truncated to 7 and not 8. To round the integer, use this:
double x = (encryptCode/255.0)*base;
int characterSteps = (int) Math.round(x);
Happy Coding! :)
Suppose I have an EditText:
Editable e = editText.getEditableText(); // length == 2
// ...attach a span start=0, end=2 to e...
int index = e.nextSpanTransition(0, 2, Object.class);
According to this: http://developer.android.com/reference/android/text/Spanned.html
index should be 0, since it says
Return the first offset greater than or equal to start where a markup object of class type begins or ends
But index is 2. Is this a bug or am I missing something?
Or am I even misinterpreting the docs, since it could mean "greater than start where a markup object begins, OR, equal to start where a markup object ends"?
The documentation also says:
or limit if there are no starts or ends greater than or equal to start but less than limit
Where limit is the second parameter (2 in your case). Your span does not satisfy less than limit because it is equal to it. So it returns limit.
Here is the source code that explains it:
/**
* Return the next offset after <code>start</code> but less than or
* equal to <code>limit</code> where a span of the specified type
* begins or ends.
*/
public int nextSpanTransition(int start, int limit, Class kind) {
int count = mSpanCount;
Object[] spans = mSpans;
int[] starts = mSpanStarts;
int[] ends = mSpanEnds;
int gapstart = mGapStart;
int gaplen = mGapLength;
if (kind == null) {
kind = Object.class;
}
for (int i = 0; i < count; i++) {
int st = starts[i];
int en = ends[i];
if (st > gapstart)
st -= gaplen;
if (en > gapstart)
en -= gaplen;
if (st > start && st < limit && kind.isInstance(spans[i]))
limit = st;
if (en > start && en < limit && kind.isInstance(spans[i]))
limit = en;
}
return limit;
}
Look at the last 2 if-sentences, in your case st=0, start=0, en=2, limit=2. The first if is false, the second if is false too. At the end it returns the unchanged limit parameter.
I wrote a simple Android application that is using MediaMetadataRetriver class to get frames. It works fine, except that I realized that it skips frames.
The video clip I am trying to decode is one shot with the phone camera. Follow relevant code snippets:
MediaMetadataRetriever mediaDataRet = new MediaMetadataRetriever();
mediaDataRet.setDataSource(path);
String lengthMsStr = mediaDataRet
.extractMetadata(mediaDataRet.METADATA_KEY_DURATION);
final long lenMs = Long.parseLong(lengthMsStr);
String widthStr = mediaDataRet
.extractMetadata(mediaDataRet.METADATA_KEY_VIDEO_WIDTH);
int width = Integer.parseInt(widthStr);
String heightStr = mediaDataRet
.extractMetadata(mediaDataRet.METADATA_KEY_VIDEO_HEIGHT);
int height = Integer.parseInt(heightStr);
note the variable lenMs, it holds the clid duration in milliseconds. Then for every frame I do:
int pace = 30; // 30 fps ms spacing
for (long i = 0; i < lenMs; i += pace) {
if (is_abort())
return;
Bitmap bitmap = mediaDataRet.getFrameAtTime(i * 1000); // I tried the other version of this method with OPTION_CLOSEST, with no luck.
if (bc == null)
bc = bitmap.getConfig();
bitmap.getPixels(pixBuffer, 0, width, 0, 0, width, height);
[...]
}
After checking visually I noticed that some frames are skipped (like short sequences). Why? And ho do I avoid this?
Use:
mediaDataRet.getFrameAtTime(i * 1000, MediaMetadataRetriever.OPTION_CLOSEST);
The getFrameAtTime(n) uses OPTION_CLOSEST_SYNC which would give you key frames only.
Now I have 2 dimension array that collects all color's pixel
and I have 1 dimension array that collects a specific color that every pixel need to check with this array. But how to check this 2 arrays
first array is
array_A = new String[bitmap.getWidth()][bitmap.getHeight()];
Another is final String[] array_B = { "ffcc33","ffcc00",....} so how can I check this 2 arrays :)) Thanks in Advance
Hope that snippets would help you. Also, you can use pixel[] = new int [width*height] to get pixel from image.
for(int w= 0; w < bitmap.getwidth(); w++)
{
for(int h = 0 ; h < bitmap,getheight() ; h++ )
{
int c = bitmap.getPixel(w, h);
Log.i("Pixels", h+"X"+w);
}
}