Skip to content

Instantly share code, notes, and snippets.

@luizmarcus
Created February 23, 2016 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizmarcus/bb401d450d0ef59ea318 to your computer and use it in GitHub Desktop.
Save luizmarcus/bb401d450d0ef59ea318 to your computer and use it in GitHub Desktop.
@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