Created
February 23, 2016 01:50
-
-
Save luizmarcus/bb401d450d0ef59ea318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EActivity(R.layout.activity_main) | |
public class MainActivity extends AppCompatActivity { | |
private static final String IMG_URL = "https://pixabay.com/static/uploads/photo/2013/10/22/03/49/waterfall-199204_960_720.jpg"; | |
@ViewById | |
protected ImageView imagem; | |
@ViewById(R.id.viewText) | |
protected TextView texto; | |
@ViewById | |
protected Button botao; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
/** | |
* Chamado após carregar as views | |
*/ | |
@AfterViews | |
void loadViews(){ | |
texto.setText("Views inicializadas"); | |
} | |
/* | |
* Chamado quando o botão é clicado | |
* */ | |
@Click(R.id.botao) | |
void baixarImagem(){ | |
texto.setText("Baixando imagem"); | |
executarDownload(); | |
} | |
/* | |
* Cria uma thread em background para executar o método | |
* */ | |
@Background | |
void executarDownload(){ | |
Bitmap img = null; | |
try { | |
InputStream in = new java.net.URL(IMG_URL).openStream(); | |
img = BitmapFactory.decodeStream(in); | |
} catch (Exception e) { | |
Log.e("Error", e.getMessage()); | |
e.printStackTrace(); | |
} | |
atualizaViews(img); | |
} | |
/** | |
* Executa o método na thread principal | |
*/ | |
@UiThread | |
void atualizaViews(Bitmap bitmap){ | |
imagem.setImageBitmap(bitmap); | |
texto.setText("Carregou!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment