Using bitwise & (AND) in ViewHolder.icon.setImageBItmap - android-viewholder

I was watching Google's I/O video on ViewHolder, but I'm lost on line 17. It looks like:
if (position & 1) {
holder.icon.setImageBitmap(mIcon1);
} else {
holder.icon.setImageBitmap(mIcon2);
}
What is the importance of if (position & 1)
1 public View getView(int position, View convertView, ViewGroup parent) {
2 ViewHolder holder;
3
4 if (convertView == null) {
5 convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
6
7 holder = new ViewHolder();
8 holder.text = (TextView) convertView.findViewById(R.id.text);
9 holder.icon = (ImageView) convertView.findViewById(R.id.icon);
10
11 convertView.setTag(holder);
12 } else {
13 holder = (ViewHolder) convertView.getTag();
14 }
15
16 holder.text.setText(DATA[position]);
17 holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
18
19 return convertView;
20 }
Slide 13
https://dl.google.com/io/2009/pres/Th_0230_TurboChargeYourUI-HowtomakeyourAndroidUIfastandefficient.pdf
https://www.youtube.com/watch?v=N6YdwzAvwOA&t=10m14s

Related

Loop forth and back through image array _ SwiftUI

I'm trying to make an animation from 5 pictures in my App. I want to click on a displayed Image and then start the animation that goes forth and back through array and ends/stops again on the first picture.
So far I've got this - I loop from 0 to 4 and then it just keep looping between 4 & 3.
Do you have any ideas how could I fix it?
Thanks!
struct ContentView: View {
#State private var activeImageIndex = 0
#State private var startTimer = false
let timer = Timer.publish(every: 0.15, on: .main, in: .common).autoconnect()
let myShots = ["MuscleUp1", "MuscleUp2", "MuscleUp3", "MuscleUp4", "MuscleUp5"]
var body: some View {
VStack {
Image(self.myShots[self.activeImageIndex])
.resizable()
.aspectRatio(contentMode: .fit)
.onReceive(self.timer) { time in
if self.startTimer {
if self.activeImageIndex != 4 {
self.activeImageIndex += 1
} else {
self.activeImageIndex -= 1
}
}
}
.onTapGesture {
self.startTimer.toggle()
}
}
.background(Color.blue)
}
}
You seem to have a problem in the if statement used to increment activeImageIndex:
if self.activeImageIndex != 4 {
self.activeImageIndex += 1
} else {
self.activeImageIndex -= 1
}
When activeImageIndex starts and successfully goes to 4, it will then run like this:
if self.activeImageIndex != 4 4 == 4, therefore enters the else case self.activeImageIndex -= 1, activeImageIndex is now 3.
if self.activeImageIndex != 4 3 != 4, therefore enters the if case self.activeImageIndex += 1, activeImageIndex is now 4 again.
Then this repeats forever.
You can add private var eg. _up
..
#State private var _up = true
......
and modify .onReceive
.onReceive(self.timer) { time in
if self.startTimer {
if self.activeImageIndex != 4 && self._up {
self.activeImageIndex += 1
if self.activeImageIndex == 4 {
self._up = false
}
} else {
self.activeImageIndex -= 1
if self.activeImageIndex == 0 {
self._up = true
}
}
}

Boost: How to add child tree into an existing tree

I have the following property tree.
propset1
{
prop1 2
prop2 5
prop3 60
prop4 7
}
I am trying to add couple of child trees to this so that the resulting property tree would look like below.
propset1
{
prop1 2
prop2 5
prop3 60
prop4 5
childset1
{
child1prop1 4
child1prop1 6
child1prop1 9
}
childset2
{
child2prop1 2
child2prop1 6
child2prop1 7
}
}
I wrote the following code.
// Add General config
pt::ptree propset1;
propset1.put("propset1.prop1", 2);
propset1.put("propset1.prop2", 5);
propset1.put("propset1.prop3", 60);
propset1.put("propset1.prop4", 7);
// Add childset1 config
pt::ptree childset1;
childset1.add("child1prop1", 4);
childset1.add("child1prop2", 6);
childset1.add("child1prop3", 9);
propset1.add_child("childset1", childset1);
// Add childset2 config
pt::ptree childset2;
childset2.add("child2prop1", 2);
childset2.add("child2prop2", 6);
childset2.add("child2prop3", 7);
propset1.add_child("childset2", childset2);
However the above code is resulting into below property tree.
propset1
{
prop1 2
prop2 5
prop3 60
prop4 5
childset1
{
child1prop1 4
child1prop1 6
child1prop1 9
}
}
propset1
{
prop1 2
prop2 5
prop3 60
prop4 5
childset2
{
child2prop1 2
child2prop1 6
child2prop1 7
}
}
Can someone please suggest what is correct use of API?
You're something else differently that you're not showing:
Live On Coliru
#include <boost/property_tree/info_parser.hpp>
#include <sstream>
#include <iostream>
using boost::property_tree::ptree;
int main() {
ptree pt;
{
std::istringstream iss(R"(propset1
{
prop1 2
prop2 5
prop3 60
prop4 7
})");
read_info(iss, pt);
}
auto& propset1 = pt.get_child("propset1");
// Add childset1 config
ptree childset1;
childset1.add("child1prop1", 4);
childset1.add("child1prop2", 6);
childset1.add("child1prop3", 9);
propset1.add_child("childset1", childset1);
// Add childset2 config
ptree childset2;
childset2.add("child2prop1", 2);
childset2.add("child2prop2", 6);
childset2.add("child2prop3", 7);
propset1.add_child("childset2", childset2);
write_info(std::cout, pt);
}
Prints:
propset1
{
prop1 2
prop2 5
prop3 60
prop4 7
childset1
{
child1prop1 4
child1prop2 6
child1prop3 9
}
childset2
{
child2prop1 2
child2prop2 6
child2prop3 7
}
}

Additional content element in TYPO3 menu

I made a menu in TypoScript in which on drop down section I have list of subpages and list of news. The code of menu is working but it render the list of news after every subpage (subpage1 and list of news, subpage 2 and list of news... etc). I would like to have two
independent element (one list of subpages and one list of news), but i don't know how to achieve that?
here is my menu code:
lib.header_main-menu = COA
lib.header_main-menu {
10 = HMENU
10 {
wrap = <ul class="nav navbar-nav">|</ul>
entryLevel = 0
1 = TMENU
1 {
noBlur = 1
wrap = |
expAll = 1
NO = 1
NO {
ATagTitle.field = title
wrapItemAndSub = <li>|</li>
stdWrap.htmlSpecialChars = 1
}
}
2 = TMENU
2 {
expAll = 1
wrap = <div class="subnav-wrapper"><ul class="subnav">|</ul></div>
NO = 1
NO {
wrapItemAndSub = <li>|</li>
wrapItemAndSub.append = COA
wrapItemAndSub.append {
wrap = |
10 = CONTENT
10 < lib.header_menu_news
}
}
}
}
lib.header_main-menu = COA
lib.header_main-menu {
wrap = <div>|</div>
// this is section 10 of the COA
10 = HMENU
10 {
wrap = <ul class="nav navbar-nav">|</ul>
entryLevel = 0
1 = TMENU
1 {
noBlur = 1
wrap = |
expAll = 1
NO = 1
NO {
ATagTitle.field = title
wrapItemAndSub = <li>|</li>
stdWrap.htmlSpecialChars = 1
}
}
2 = TMENU
2 {
expAll = 1
wrap = <div class="subnav-wrapper"><ul class="subnav">|</ul></div>
NO = 1
NO {
wrapItemAndSub = <li>|</li>
}
}
}
// this is section 20 of the COA
// btw the next line isn't necessary, you do that (define the object type) in lib.header_menu_news already
// 10 (would be 20 now) = CONTENT
20 < lib.header_menu_news
20.wrap = <h3>News</h3>|
}
actually, this comes to the same as putting them into the template separately
<div>###MENU###
<h3>News</h3>
###NEWSMENU###</div>
btw unless you use the lib.object in the Backend somewhere, I would use temp. instead - the latter are unset after building the page object, and my guess would be that this might be better for performance.

Random Image and get winner (Processing)

Hi I'm building a slot machine game and am having a few problems I'm pretty new with processing.
I've made this code and need the "wheels" to be random but they all have the same image (vertically)
I realise you won't be able to see the images so here's some links if you want them.
http://tinypic.com/r/i1jq7t/6 (diamond1)
http://tinypic.com/r/29fe5cg/6 (cherry1)
http://tinypic.com/r/sen1jo/6 (bell1)
int numFrames = 3; // The number of frames in the animation
int frame = 0;
int spincount = 0;
int state = 0;
PImage[] images1 = new PImage[3];
PImage[] images2 = new PImage[3];
PImage[] images3 = new PImage[3];
void setup() {
size(1080, 720);
frameRate(12);
// wheel 1
images1[0] = loadImage("bell1.png");
images1[1] = loadImage("cherry1.png");
images1[2] = loadImage("diamond1.png");
// wheel 3
images3[0] = loadImage("cherry1.png");
images3[1] = loadImage("bell1.png");
images3[2] = loadImage("diamond1.png");
// wheel 2
images2[0] = loadImage("diamond1.png");
images2[1] = loadImage("bell1.png");
images2[2] = loadImage("cherry1.png");
}
void draw() {
background(o);
//test state to see if I should be spinning
if(state == 1) {
spin();
}
}
//if a key is pressed then set the wheel to spin
void keyReleased() {
state = 1;
}
void spin() {
//spin for 5 times the break out
if (frame == 3) {
frame = 0;
spincount ++;
if (spincount == 10) {
state = 0;
spincount = 0;
//check if its a win and do stuff
winner();
}
}
// wheel 1
image(images1[frame], 20, 0);
image(images1[frame], 20, 170); //this is the image to test
image(images1[frame], 20, 340);
// wheel 2
image(images3[frame], 200, 0);
image(images3[frame], 200, 170); //this is the image to test
image(images3[frame], 200, 340);
// wheel 3
image(images2[frame], 400, 0);
image(images2[frame], 400, 170); //this is the image to test
image(images2[frame], 400, 340);
frame ++;
}
void winner() {
//are the names of the images the same
//if ((images3[frame].get(0,0)) == (images2[frame].get(0,0)) == (images1[frame].get(0,0))) {
// display a question from you list of questions by generating a random number and selecting the text
// wait for an answer
for (int i = 0; i < 400; i = i+1) {
if (keyPressed == true) {
// whats the key is it correct
}
if (i > 400) {
//display times up
}
}
}
// }
I'm also having issues with getting the "winner" (if the horizontal images's pixel in the left hand corner match go onto "winner".
I'd really appreciate any help anyone can offer.
There are a few problems with your code.
In the draw function, you are probably wanting to draw a black background, but you currently have the letter "o" instead of the number 0 (zero).
You probably want to draw the current wheel state to the screen in your draw function, right now you just have a black window unless you are spinning.
You can't compare colors directly.
Here is some modified code to get it working:
int numFrames = 3; // The number of frames in the animation
int maxSpin = 10;
int frame = 0;
int spincount = 0;
boolean spinning = false;
boolean checkWinner = false;
PImage[] images1 = new PImage[3];
PImage[] images2 = new PImage[3];
PImage[] images3 = new PImage[3];
void setup() {
size(1080, 720);
frameRate(12);
// wheel 1
images1[0] = loadImage("bell1.png");
images1[1] = loadImage("cherry1.png");
images1[2] = loadImage("diamond1.png");
// wheel 3
images3[0] = loadImage("cherry1.png");
images3[1] = loadImage("bell1.png");
images3[2] = loadImage("diamond1.png");
// wheel 2
images2[0] = loadImage("diamond1.png");
images2[1] = loadImage("bell1.png");
images2[2] = loadImage("cherry1.png");
}
void draw() {
background(0);
//test state to see if I should be spinning
// wheel 1
image(images1[frame], 20, 0);
image(images1[frame], 20, 170); //this is the image to test
image(images1[frame], 20, 340);
// wheel 2
image(images3[frame], 200, 0);
image(images3[frame], 200, 170); //this is the image to test
image(images3[frame], 200, 340);
// wheel 3
image(images2[frame], 400, 0);
image(images2[frame], 400, 170); //this is the image to test
image(images2[frame], 400, 340);
if (spinning) {
spin();
}
//this draws circles stroked in the color of the pixel at their center
//this is just to show how the code works, you can remove this
noFill(); // make the circles transparent
loadPixels(); // required before using pixels[]
color wheel1 = pixels[170*width+20];
color wheel2 = pixels[170*width+200];
color wheel3 = pixels[170*width+400];
stroke(wheel1);
ellipse(20, 170, 10, 10);
stroke(wheel2);
ellipse(200, 170, 10, 10);
stroke(wheel3);
ellipse(400, 170, 10, 10);
}
//if a key is pressed then set the wheel to spin
void keyReleased() {
if(!spinning) spinning = !spinning;
}
void spin() {
//spin for maxSpin times then break out
if (frame == numFrames-1) {
spincount ++;
if (spincount == maxSpin) {
spinning = !spinning;
spincount = 0;
//check if its a win and do stuff
winner();
}
else frame = 0;
}
else frame++;
}
void winner() {
loadPixels();
color wheel1 = pixels[171*width+21];
color wheel2 = pixels[171*width+201];
color wheel3 = pixels[171*width+401];
if(hue(wheel1) == hue(wheel2) && hue(wheel2) == hue(wheel3)) println("winner");
else println("not a winner");
}
This inclues your desired method of checking the corner colors on the images, but I would recommend just keeping track of which image is currently being displayed in the center.
I hope this at least gets you moving in the right direction.

Sudoku algorithm, brute force [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Iam trying to solve a sudoku board with a brute force algorithm, I cant really get this algorithm work correctly.
There is created a object for each row, column and box that contains all squares(cells) that belongs to the actually column, square and row, this is used in legalValue() to check if value can be placed in the cell.
I cant find the structure that make the algorithm to work.
boolean setNumberMeAndTheRest(Board board) {
if(getNext() == null) {
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
}
}
board.saveSolution();
} else {
if(this instanceof DefinedSquare) {
getNext().setNumberMeAndTheRest(board);
} else {
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
if(getNext().setNumberMeAndTheRest(board)) {
return true;
} else {
setValue(i);
}
}
}
return false;
}
}
return false;
}
Here is legalValue(int i);
/**
* Checks if value is legal in box, row and column.
* #param value to check.
* #return true if value is legal, else false.
*/
boolean legalValue(int value) {
if(box.legalValue(value) && row.legalValue(value) && columne.legalValue(value)) {
return true;
}
return false;
}
**4x4 Sudoku board INPUT**
0 2 | 1 3
0 0 | 0 4
---------
0 0 | 0 1
0 4 | 3 2
**Expected OUTPUT**
4 2 | 1 3
3 1 | 2 4
---------
2 3 | 4 1
1 4 | 3 2
**Actually OUTPUT**
4 2 | 1 3
2 4 | 3 4
---------
3 0 | 0 1
0 4 | 3 2
Added resetting of board
boolean setNumberMeAndTheRest(Board board) {
Board original = board;
if(getNext() == null) {
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
}
}
board.saveSolution();
} else {
if(this instanceof DefinedSquare) {
getNext().setNumberMeAndTheRest(board);
} else {
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
if(getNext().setNumberMeAndTheRest(board)) {
return true;
} else {
setValue(i);
}
}
}
board = original;
return false;
}
}
board = original;
return false;
}
Her is a solution, after a long time :D
boolean setNumberMeAndTheRest(Board board) {
if(next == null) {
board.saveSolution();
return true;
}
if(this instanceof DefinedSquare) {
return next.setNumberMeAndTheRest(board);
}
for(int i = 1; i <= board.getDimension(); ++i) {
if(legalValue(i)) {
setValue(i);
if(next.setNumberMeAndTheRest(board)) {
return true;
}
}
}
setValue(0);
return false;
}
boolean setNumberMeAndTheRest(Board board) {
// make a copy of the original board
Board original = board;
then every time you return false, you also need to reset the board to its original state
board = original;
return false;

Resources