晕晕晕!
Jadipng itu adalah网站yang menyediakan gambar — gambar dengan resolusi yang tinggi,免费的Menyediakan API。 不,帕达教程卡利·伊尼(Kali ini),阿坎(Akan)佛教徒阿普尔卡西·塞德勒哈纳(Akan dibuat)
Pertama,jangan lupa kasih许可互联网karena kita阿寒melakukan GET请求ke
AndroidManifest.xml
<? xml版本=“ 1.0”编码=“ utf-8” ?>
< 清单xmlns:android =” http://schemas.android.com/apk/res/android”
包裹=“ id.prihantoro.pixabay” >
< Uses-permission android:name =” android.permission.INTERNET” />
< 应用
android:allowBackup =” true”
android:icon =” @ mipmap / ic_launcher”
android:label =” @ string / app_name”
android:supportsRtl =“ true”
android:theme =” @ style / AppTheme” >
< 活动android:name =”。MainActivity_” >
< 意图过滤器 >
< action android:name =” android.intent.action.MAIN” />
< category android:name =” android.intent.category.LAUNCHER” />
</ intent-filter >
</ 活动 >
</ application >
</ 清单 >
击中
Hit.java
包 id.prihantoro.pixabay.model;
导入 com.google.gson.annotations.Expose;
导入 com.google.gson.annotations.SerializedName;
/ **
*由wahyu在2016年12月10日创建。
* /
公开课热门{
@SerializedName( “ previewHeight” )
@暴露
private int PreviewHeight ;
@SerializedName( “喜欢” )
@暴露
私人int喜欢 ;
@SerializedName( “收藏夹” )
@暴露
私人int的最爱 ;
@SerializedName( “标签” )
@暴露
私有字符串标签 ;
@SerializedName( “ webformatHeight” )
@暴露
private int webformatHeight ;
@SerializedName( “ views” )
@暴露
私人诠释 ;
@SerializedName( “ webformatWidth” )
@暴露
private int webformatWidth ;
@SerializedName( “ previewWidth” )
@暴露
private int PreviewWidth ;
@SerializedName( “评论” )
@暴露
私人诠释 ;
@SerializedName( “下载” )
@暴露
私人int下载 ;
@SerializedName( “ pageURL” )
@暴露
私有 String pageURL ;
@SerializedName( “ previewURL” )
@暴露
私有 String PreviewURL ;
@SerializedName( “ webformatURL” )
@暴露
私有字符串webformatURL ;
@SerializedName( “ imageWidth” )
@暴露
private int imageWidth ;
@SerializedName( “ user_id” )
@暴露
private int userId ;
@SerializedName( “用户” )
@暴露
私有字符串用户 ;
@SerializedName( “ type” )
@暴露
私有字符串类型 ;
@SerializedName( “ id” )
@暴露
私有int id ;
@SerializedName( “ userImageURL” )
@暴露
私有 String userImageURL ;
@SerializedName( “ imageHeight” )
@暴露
private int imageHeight ;
/ **
* @return 预览高度
* /
public int getPreviewHeight(){
返回PreviewHeight ;
}
/ **
* @param PreviewHeight 预览高度
* /
public void setPreviewHeight( int PreviewHeight){
这个 。 PreviewHeight = PreviewHeight;
}
/ **
* @return 喜欢
* /
public int getLikes(){
返回喜欢 ;
}
/ **
* @param 喜欢
* /
public void setLikes( int likes){
这个 。 喜欢 =喜欢;
}
/ **
* @return 收藏
* /
public int getFavorites(){
返回收藏夹 ;
}
/ **
* @param的收藏 夹
* /
public void setFavorites( int favorite){
这个 。 最爱 =最爱;
}
/ **
* @return 标签
* /
公共字符串getTags(){
返回标签 ;
}
/ **
* @param 标签
* /
公共无效 setTags(字符串标签){
这个 。 标签 =标签;
}
/ **
* @return webformatHeight
* /
public int getWebformatHeight(){
返回webformatHeight ;
}
/ **
* @param webformatHeight webformatHeight
* /
public void setWebformatHeight( int webformatHeight){
这个 。 webformatHeight = webformatHeight;
}
/ **
* @return 意见
* /
public int getViews(){
返回意见 ;
}
/ **
* @param views 视图
* /
public void setViews( int views){
这个 。 views =视图;
}
/ **
* @return webformatWidth
* /
public int getWebformatWidth(){
返回webformatWidth ;
}
/ **
* @param webformatWidth webformatWidth
* /
公共无效 setWebformatWidth( int webformatWidth){
这个 。 webformatWidth = webformatWidth;
}
/ **
* @return the PreviewWidth
* /
public int getPreviewWidth(){
返回PreviewWidth ;
}
/ **
* @param PreviewWidth 预览宽度
* /
public void setPreviewWidth( int PreviewWidth){
这个 。 PreviewWidth = PreviewWidth;
}
/ **
* @return 评论
* /
public int getComments(){
返回评论 ;
}
/ **
* @param评论 评论
* /
public void setComments( int comments){
这个 。 评论 =评论;
}
/ **
* @return 下载
* /
public int getDownloads(){
返回下载 ;
}
/ **
* @param下载 下载
* /
public void setDownloads( int downloads){
这个 。 下载 =下载;
}
/ **
* @return pageURL
* /
公共字符串getPageURL(){
返回pageURL ;
}
/ **
* @param pageURL pageURL
* /
public void setPageURL(String pageURL){
这个 。 pageURL = pageURL;
}
/ **
* @return 预览URL
* /
公共字符串getPreviewURL(){
返回PreviewURL ;
}
/ **
* @param PreviewURL 预览URL
* /
public void setPreviewURL(String PreviewURL){
这个 。 PreviewURL = PreviewURL;
}
/ **
* @return webformatURL
* /
公共字符串getWebformatURL(){
返回webformatURL ;
}
/ **
* @param webformatURL webformatURL
* /
公共无效 setWebformatURL(String webformatURL){
这个 。 webformatURL = webformatURL;
}
/ **
* @return imageWidth
* /
public int getImageWidth(){
返回imageWidth ;
}
/ **
* @param imageWidth imageWidth
* /
公共无效 setImageWidth( int imageWidth){
这个 。 imageWidth = imageWidth;
}
/ **
* @return userId
* /
public int getUserId(){
返回userId ;
}
/ **
* @param userId user_id
* /
公共无效 setUserId( int userId){
这个 。 userId = userId;
}
/ **
* @return 用户
* /
公共字符串getUser(){
回头客 ;
}
/ **
* @param用户 用户
* /
public void setUser(String user){
这个 。 用户 =用户;
}
/ **
* @return 类型
* /
公共字符串getType(){
返回类型 ;
}
/ **
* @param type 类型
* /
public void setType(String type){
这个 。 类型 =类型;
}
/ **
* @返回 ID
* /
public int getId(){
返回ID ;
}
/ **
* @param id id
* /
公共无效 setId( int id){
这个 。 id = id;
}
/ **
* @return userImageURL
* /
公共字符串getUserImageURL(){
返回userImageURL ;
}
/ **
* @param userImageURL userImageURL
* /
公共无效 setUserImageURL(String userImageURL){
这个 。 userImageURL = userImageURL;
}
/ **
* @return imageHeight
* /
public int getImageHeight(){
返回imageHeight ;
}
/ **
* @param imageHeight imageHeight
* /
公共无效 setImageHeight( int imageHeight){
这个 。 imageHeight = imageHeight;
}
}
响应
响应程序
包 id.prihantoro.pixabay.model;
导入 com.google.gson.annotations.Expose;
导入 com.google.gson.annotations.SerializedName;
导入 java.util.List;
/ **
*由wahyu在2016年12月10日创建。
* /
公共类 PixelResponse {
@SerializedName( “ totalHits” )
@暴露
私人int totalHits ;
@SerializedName( “ hits” )
@暴露
私人 List hits = null ;
@SerializedName( “ total” )
@暴露
私有int总数 ;
/ **
* @return 总命中数
* /
public int getTotalHits(){
返回totalHits ;
}
/ **
* @param totalHits 总命中数
* /
公共无效 setTotalHits( int totalHits){
这个 。 totalHits = totalHits;
}
/ **
* @return 命中
* /
公共列表 getHits(){
返回命中 ;
}
/ **
* @param hits 命中
* /
公共无效 setHits(List hits){
这个 。 点击数 =点击数;
}
/ **
* @return 总数
* /
public int getTotal(){
回报总额 ;
}
/ **
* @param总计 总计
* /
public void setTotal( int total){
这个 。 总数 =总数;
}
}
改造单身
Retrofit.java
软件包 id.prihantoro.pixabay.api;
导入 android.content.Context;
导入 org.androidannotations.annotations.EBean;
导入 org.androidannotations.annotations.RootContext;
导入 okhttp3.OkHttpClient;
导入 retrofit2.converter.gson.GsonConverterFactory;
/ **
*由wahyu在2016年12月10日创建。
* /
@EBean
公共类翻新{
字符串baseUrl = “ https://pixabay.com/” ;
@RootContext
语境语境 ;
公共 T getService(Class serviceClass){
返回 getRetrofit()。create(serviceClass);
}
公共同步 Retrofit2.Retrofit getRetrofit(){
OkHttpClient.Builder httpClient = 新的 OkHttpClient.Builder();
OkHttpClient客户端= httpClient.build();
返回新的 Retrofit2.Retrofit
.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory。create())
.client(客户)
。建立();
}
}
界面服务
pngService.java
软件包 id.prihantoro.pixabay.api.service;
导入 java.util.Map;
导入 id.prihantoro.pixabay.model.PixabayResponse;
导入 retrofit2.Call;
导入 retrofit2.http.GET;
导入 retrofit2.http.QueryMap;
/ **
*由wahyu在2016年12月10日创建。
* /
公用接口 pngServie {
@GET( “ api” )
调用 getImages(@QueryMap Map 参数);
}
适配器
适配器文件
软件包 id.prihantoro.pixabay.adapter;
导入 android.content.Context;
导入 android.support.v7.widget.CardView;
导入 android.support.v7.widget.RecyclerView;
导入 android.view.LayoutInflater;
导入 android.view.View;
导入 android.view.ViewGroup;
导入 android.widget.ImageView;
导入 com.bumptech.glide.Glide;
导入 java.util.List;
导入 id.prihantoro.pixabay.R;
导入 id.prihantoro.pixabay.model.Hit;
/ **
*由wahyu在2016年12月10日创建。
* /
公共类 pngadapter 扩展了 RecyclerView.Adapter {
私人语境;
私人 List 命中 ;
publicpngAdapter(Context context,List hits){
这个 。 上下文 =上下文
这个 。 点击数 =点击数;
}
@Override
public HitViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
查看itemView = LayoutInflater。 来自 (parent.getContext())
.inflate(R.layout。hit_item,parent, false );;
返回新的 HitViewHolder(itemView);
}
@Override
public void onBindViewHolder(HitViewHolder持有人, int位置){
Hit hit = hits .get(position);
滑行。 与 ( context ).load(hit.getWebformatURL())。fitCenter()。into(holder.image);
}
@Override
public int getItemCount(){
返回点击 .size();
}
公共无效 addItem(命中率){
点击 .add(hit);
notifyDataSetChanged();
}
公共无效的 addItems(列表命中数){
hits.addAll(hits);
notifyDataSetChanged();
}
HitViewHolder 类 扩展了 RecyclerView.ViewHolder {
公共 CardView 简历 ;
公共 ImageView 图像 ;
公共 HitViewHolder(查看itemView){
超级 (itemView);
image =(ImageView)itemView.findViewById(R.id。image);
cv =(CardView)itemView.findViewById(R.id。cv);
}
}
}
主要活动
MainActivity.java
软件包 id.prihantoro.pixabay;
导入 android.support.v7.app.AppCompatActivity;
导入 android.support.v7.widget.LinearLayoutManager;
导入 android.support.v7.widget.RecyclerView;
导入 android.widget.Toast;
导入 org.androidannotations.annotations.AfterViews;
导入 org.androidannotations.annotations.Bean;
导入 org.androidannotations.annotations.EActivity;
导入 org.androidannotations.annotations.ViewById;
导入 java.util.HashMap;
导入 java.util.List;
导入 id.prihantoro.pixabay.adapter.PixabayAdapter;
导入 id.prihantoro.pixabay.api.Retrofit;
导入 id.prihantoro.pixabay.api.service.PixabayServie;
导入 id.prihantoro.pixabay.model.Hit;
导入 id.prihantoro.pixabay.model.PixabayResponse;
导入 retrofit2.Call;
导入 retrofit2.Callback;
导入更新 2.Response;
/ **
*由wahyu在2016年12月10日创建。
* /
@EActivity(R.layout.activity_main)
公共类 MainActivity 扩展了 AppCompatActivity {
@ViewById
RecyclerView rv ;
@豆角,扁豆
改造改造 ;
适配器适配器 ;
int currentPage = 0;
HashMap 映射 = 新的 HashMap ();
@AfterViews
无效的 init(){
映射 .put( “ key” , “ 3968517-94dbe52e08b9ec52249a64fdc” );
映射 .put( “ q” , “ animal” );
map .put( “ image_type” , “ all” );
映射 .put( “ page” , “ 1” );
调用调用= 改装 .getService(PixabayServie.class).getImages( map );
call.enqueue( new Callback (){
@Override
public void onResponse(Call 调用,响应响应){
响应=响应.body();
List 命中= PixabayResponse.getHits();
adapter = newPixabaAdapter(getApplicationContext(),hits);
rv .setAdapter( 适配器 );
rv .setLayoutManager( new LinearLayoutManager(getApplicationContext()));
currentPage = 1;
}
@Override
public void onFailure(Call call,Throwable t){
吐司 makeText (MainActivity.this,t.getMessage(), Toast.LENGTH_SHORT ).show();
}
});
}
公共无效 loadNextPage(){
map .put( “ page” ,( currentPage +1)+ “” );
调用调用= 改装 .getService(PixabayServie.class).getImages( map );
call.enqueue( new Callback (){
@Override
public void onResponse(Call 调用,响应响应){
响应=响应.body();
List 命中= PixabayResponse.getHits();
适配器 .addItems(hits);
currentPage ++;
}
@Override
public void onFailure(Call call,Throwable t){
吐司 makeText (MainActivity.this,t.getMessage(), Toast.LENGTH_SHORT ).show();
}
});
}
}
命中项目视图
hit_item.xml
<? xml version =“ 1.0” encoding =“ utf-8” 吗?>
< LinearLayout xmlns:android =“ http://schemas.android.com/apk/res/android”
android:layout_width =“ match_parent”
android:layout_height =“ wrap_content”
android:orientation =“ vertical” >
< android.support.v7.widget.CardView
android:id =“ @ + id / cv”
android:layout_width =“ match_parent”
android:layout_height =“ wrap_content”
android:layout_marginLeft =“ 5dp”
android:layout_marginRight =“ 5dp”
android:layout_marginTop =“ 5dp”
android:padding =“ 5dp” >
< ImageView
android:id =“ @ + id / image”
android:layout_width =“ match_parent”
android:layout_height =“ wrap_content” />
</ android.support.v7.widget.CardView >
</ LinearLayout >
主要活动布局
activitty_main.xml
<? xml version =“ 1.0” encoding =“ utf-8” 吗?>
< LinearLayout xmlns:android =“ http://schemas.android.com/apk/res/android”
xmlns:tools =“ http://schemas.android.com/tools”
android:id =“ @ + id / activity_main”
android:layout_width =“ match_parent”
android:layout_height =“ match_parent”
工具:context =“ id.prihantoro.pixabay.MainActivity” >
< android.support.v7.widget.RecyclerView
android:id =“ @ + id / rv”
android:layout_width =“ match_parent”
android:layout_height =“ wrap_content” > </ android.support.v7.widget.RecyclerView >
</ LinearLayout >
摇篮
build.gradle
应用插件 : “ com.android.application”
应用插件 : “ com.neenbedankt.android-apt”
def AAVersion = '4.0.0'
android {
编译版本25
buildToolsVersion “ 25.0.0”
defaultConfig {
applicationId “ id.prihantoro.pixabay”
minSdkVersion 16
targetSdkVersion 25
版本代码1
versionName “ 1.0”
testInstrumentationRunner “ android.support.test.runner.AndroidJUnitRunner”
}
buildTypes {
发布 {
minifyEnabled false
proguardFiles getDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro'
}
}
}
buildscript {
储存库{
mavenCentral()
}
依赖项{
//替换为当前版本的Android插件
类路径'com.android.tools.build:gradle:2.2.2'
//最新版本的android-apt插件
classpath'com.neenbedankt.gradle.plugins :android-apt:1.8'
}
}
储存库{
mavenCentral()
mavenLocal()
jcenter()
}
依赖项{
apt “ org.androidannotations:androidannotations: $ AAVersion ”
编译“ org.androidannotations:androidannotations-api: $ AAVersion ”
编译fileTree( dir : 'libs' , include :[ '* .jar' ])
androidTestCompile( 'com.android.support.test.espresso:espresso-core:2.2.2' ,{
排除组 : “ com.android.support” , 模块 : “ support-annotations”
})
编译'com.android.support:appcompat-v7:25.0.0'
testCompile'junit :junit:4.12'
编译'com.android.support:cardview-v7:25.0.0'
编译'com.android.support:recyclerview-v7:25.0.0'
编译'com.github.bumptech.glide:glide:3.7.0'
编译'com.squareup.retrofit2:retrofit:2.1.0'
编译'com.google.code.gson:gson:2.4'
编译'com.squareup.retrofit2:converter-gson:2.1.0'
}
Oke langsung saja,源代码dapat diakses pada链接berikut https://github.com/wahyuprihantoro/pixabay-image-loader-apps