
{"id":6080,"date":"2017-06-11T11:29:15","date_gmt":"2017-06-11T11:29:15","guid":{"rendered":"http:\/\/tapchicntt.com\/?p=6080"},"modified":"2019-07-20T04:12:01","modified_gmt":"2019-07-20T04:12:01","slug":"androi-load-anh-tu-internet-voi-thu-vien-picasso","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/androi-load-anh-tu-internet-voi-thu-vien-picasso\/","title":{"rendered":"[Android] Load \u1ea3nh t\u1eeb internet v\u1edbi th\u01b0 vi\u1ec7n Picasso"},"content":{"rendered":"\n<p>Sau \u0111\u00e2y t\u00f4i s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n Picasso \u0111\u1ec3 load \u1ea3nh t\u1eeb internet trong l\u1eadp tr\u00ecnh android. Picasso l\u00e0 m\u1ed9t th\u01b0 vi\u1ec7n m\u00e3 ngu\u1ed3n m\u1edf \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i trong android. N\u00f3 \u0111\u01b0\u1ee3c t\u1ea1o ra b\u1edfi Square. Picasso l\u00e0 m\u1ed9t trong nh\u1eefng th\u01b0 vi\u1ec7n m\u1ea1nh m\u1ebd nh\u1ea5t trong vi\u1ec7c download v\u00e0 cache \u1ea3nh cho android.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">T\u1ea1o sao ph\u1ea3i s\u1eed d\u1ee5ng Picasso<\/h3>\n\n\n\n<p>N\u00f3 \u0111\u01a1n gi\u1ea3n ho\u00e1 qu\u00e1 tr\u00ecnh t\u1ea3i h\u00ecnh \u1ea3nh t\u1eeb url b\u00ean ngo\u00e0i v\u00e0 hi\u1ec3n th\u1ecb tr\u00ean \u1ee9ng d\u1ee5ng c\u1ee7a b\u1ea1n. V\u00ed d\u1ee5, t\u1ea3i h\u00ecnh \u1ea3nh t\u1eeb m\u00e1y ch\u1ee7, l\u00e0 m\u1ed9t trong nh\u1eefng nhi\u1ec7m v\u1ee5 ph\u1ed5 bi\u1ebfn nh\u1ea5t trong b\u1ea5t k\u1ef3 \u1ee9ng d\u1ee5ng. V\u00e0 n\u00f3 c\u1ea7n kh\u00e1 nhi\u1ec1u d\u00f2ng code ph\u1ee9c t\u1ea1p \u0111\u1ec3 l\u00e0m \u0111\u01b0\u1ee3c \u0111i\u1ec1u n\u00e0y. B\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng Picasso, b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c \u0111i\u1ec1u n\u00e0y v\u1edbi v\u00e0i d\u00f2ng m\u00e3.<\/p>\n\n\n\n<p>N\u00f3 kh\u00f4ng ph\u1ea3i li\u00ean t\u1ee5c t\u1ea3i l\u1ea1i h\u00ecnh \u1ea3nh t\u1eeb internet. Picasso cung c\u1ea5p m\u1ed9t b\u1ed9 nh\u1edb \u0111\u1ec7m \u0111\u1ec3 l\u01b0u cache nh\u1eefng h\u00ecnh \u1ea3nh \u0111\u00e3 t\u1ea3i tr\u01b0\u1edbc \u0111\u00f3, \u0111i\u1ec1u n\u00e0y gi\u00fap t\u0103ng tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng h\u01a1n<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">S\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n Picasso:<\/h3>\n\n\n\n<p>\u0110\u1ea7u ti\u00ean c\u00e1c b\u1ea1n t\u1ea3i file JAR <a href=\"http:\/\/square.github.io\/picasso\/\" target=\"_blank\" rel=\"noopener noreferrer\">th\u01b0 vi\u1ec7n Picasso<\/a> n\u1ebfu b\u1ea1n ch\u01b0a c\u00f3 n\u00f3. N\u1ebfu b\u1ea1n \u0111ang s\u1eed d\u1ee5ng Eclipse l\u00e0 IDE th\u00ec b\u1ea1n c\u1ea7n ph\u1ea3i sao ch\u00e9p t\u1eadp tin picasso-2.x.x.jar v\u00e0o th\u01b0 m\u1ee5c lib trong \u1ee9ng d\u1ee5ng c\u1ee7a b\u1ea1n. N\u1ebfu b\u1ea1n \u0111ang s\u1eed d\u1ee5ng Android studio b\u1ea1n ch\u1ec9 vi\u1ec7c import th\u01b0 hi\u1ec7n Picasso v\u00e0o t\u1eadp tin build.gradle c\u1ee7a Module:app<\/p>\n\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">compile 'com.squareup.picasso:picasso:2.5.2'<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Load h\u00ecnh \u1ea3nh t\u1eeb URL v\u00e0o ImageView<\/h3>\n\n\n\n<p>Sau \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5 trong vi\u1ec7c s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n Picasso \u0111\u1ec3 load h\u00ecnh \u1ea3nh t\u1eeb URL v\u00e0 g\u00e1n cho ImageView<\/p>\n\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;ImageView\n    android:layout_width=&quot;wrap_content&quot;\n    android:layout_height=&quot;wrap_content&quot;\n    android:id=&quot;@+id\/imageView&quot;\n    android:layout_alignParentTop=&quot;true&quot;\n    android:layout_centerHorizontal=&quot;true&quot;&gt;\n&lt;\/ImageView&gt;\n<\/pre>\n\n\n\n<p>C\u1ea5p quy\u1ec1n truy c\u1eadp internet cho \u1ee9ng d\u1ee5ng<\/p>\n\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt;\n<\/pre>\n\n\n\n<p>B\u00e2y gi\u1edd ti\u1ebfn h\u00e0nh load h\u00ecnh \u1ea3nh v\u00e0 g\u00e1n cho ImageView<\/p>\n\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/Initialize ImageView\nImageView imageView = (ImageView) findViewById(R.id.imageView);\n\/\/Loading image from below url into imageView\nPicasso.with(this)\n   .load(&quot;D\u00e1n link h\u00ecnh \u1ea3nh v\u00e0o \u0111\u00e2y&quot;)\n   .into(imageView);\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Placeholder v\u00e0 Error Fallback<\/h3>\n\n\n\n<p>Trong \u0111o\u1ea1n code tr\u00ean, ch\u00fang ta ch\u1ec9 c\u1ea7n t\u1ea3i h\u00ecnh \u1ea3nh v\u1ec1 v\u00e0 hi\u1ec3n th\u1ecb l\u00ean ImageView. Nh\u01b0ng \u0111i\u1ec1u \u0111\u00f3 l\u00e0 kh\u00f4ng \u0111\u1ee7. \u0110\u1ed1i v\u1edbi b\u1ea5t k\u1ef3 \u1ee9ng d\u1ee5ng th\u1eddi gian th\u1ef1c n\u00e0o b\u1ea1n ph\u1ea3i suy ngh\u0129 t\u1ea5t c\u1ea3 c\u00e1c tr\u01b0\u1eddng h\u1ee3p c\u00f3 th\u1ec3 x\u1ea3y ra. B\u00e2y gi\u1edd ch\u00fang ta c\u1ea7n ph\u1ea3i c\u00f3 Placeholder v\u00e0 Error Fallback.<\/p>\n\n\n\n<p>H\u00ecnh \u1ea3nh cho Placeholder s\u1ebd hi\u1ec3n th\u1ecb tr\u01b0\u1edbc khi h\u00ecnh \u1ea3nh \u0111\u01b0\u1ee3c t\u1ea3i.<br>H\u00ecnh \u1ea3nh Error Fallback s\u1ebd \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb n\u1ebfu c\u00f3 l\u1ed7i trong qu\u00e1 tr\u00ecnh t\u1ea3i \u1ea3nh.<\/p>\n\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nPicasso.with(this)\n    .load(&quot;YOUR IMAGE URL HERE&quot;)\n    .placeholder(R.drawable.ic_placeholder) \/\/ optional\n    .error(R.drawable.ic_error_fallback)         \/\/ optional\n    .into(imageView);\n<\/pre>\n\n\n\n<p>Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sau \u0111\u00e2y t\u00f4i s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n Picasso \u0111\u1ec3 load \u1ea3nh t\u1eeb internet trong l\u1eadp [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":6761,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[208],"tags":[],"class_list":["post-6080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lap-trinh-android"],"views":574,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/6080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=6080"}],"version-history":[{"count":0,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/6080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/6761"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=6080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=6080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=6080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}