? 作者介绍:友友们好我是进击攻城狮,大二学生党一枚
?作者主页:进击攻城狮的个人博客主页.进击攻城狮
?我和友友们一样喜欢编辑,天天敲代码,沉迷学习,日渐消瘦。很荣幸能向大家分享我的所学,和大家一起进步。如果文章有错误,欢迎在评论区指正。那么开始今天的学习吧!
? 支持我:点赞?+收藏⭐️+留言?
Android Studio第14课图片渐进度,播放视频,App打包
一、百度地图1.导包,添加依赖2.在AndroidManifest.xml中获取权限配置3.key值获取4.新建一个DemoApplication类5.xml中配置6.代码展示7.运行效果 二、获取相册和拍照1.导包添加依赖2.在AndroidManifest.xml中获取权限配置3.新建一个GlideEngine类4.运行代码5.效果展示 总结
一、百度地图
1.导包,添加依赖
在build.gradle导包
ndk { // 设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so) abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","x86_64" } implementation 'com.baidu.lbsyun:BaiduMapSDK_Map:7.4.0' implementation 'com.baidu.lbsyun:BaiduMapSDK_Search:7.4.0' implementation 'com.baidu.lbsyun:BaiduMapSDK_Util:7.4.0' implementation 'com.baidu.lbsyun:BaiduMapSDK_Location_All:9.1.8' implementation 'com.guolindev.permissionx:permissionx:1.6.4'
2.在AndroidManifest.xml中获取权限配置
<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 这个权限用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 这个权限用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!--具备摄像头-->
还要加入以下两个配置
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="SUbHVf89GM0GAxi0eubyrWExTxvz2GV1" /> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>
其中这个key值的来源及其重要,在百度地图开放平台
拿到这个key值
3.key值获取
进入百度地图开放平台后创建应用
应用类型选择这个Android SDK
在控制命令台.android路径下:
keytool -list -v -keystore debug.keystore
密钥口令为android
得到我们需要的SHA1
将得到的SHA1和包名(AndroidManifest.xml里面)填上去,就可以得到key值了
4.新建一个DemoApplication类
package com.hnucm.baidumap;import android.app.Application;import com.baidu.mapapi.CoordType;import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.utils.CoordinateConverter;public class DemoApplication extends Application { @Override public void onCreate() { super.onCreate(); //在使用SDK各组件之前初始化context信息,传入ApplicationContext SDKInitializer.initialize(this); //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型. //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。 SDKInitializer.setCoordType(CoordType.BD09LL); }}
5.xml中配置
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" tools:ignore="MissingClass" />
6.代码展示
package com.hnucm.baidumap;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.Manifest;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.Toast;import com.baidu.location.BDAbstractLocationListener;import com.baidu.location.BDLocation;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.MapStatus;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.MyLocationData;import com.baidu.mapapi.model.LatLng;import com.bumptech.glide.Glide;import com.luck.picture.lib.basic.PictureSelector;import com.luck.picture.lib.config.SelectMimeType;import com.luck.picture.lib.entity.LocalMedia;import com.luck.picture.lib.interfaces.OnResultCallbackListener;import com.permissionx.guolindev.PermissionX;import com.permissionx.guolindev.callback.RequestCallback;import java.io.File;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { MapView mapView; BaiduMap baiduMap; LocationClient locationClient; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PermissionX.init(this) .permissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION) .request(new RequestCallback() { @Override public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) { if (allGranted) { Toast.makeText(MainActivity.this, "All permissions are granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "These permissions are denied: $deniedList", Toast.LENGTH_LONG).show(); } } }); mapView = findViewById(R.id.bmapView); baiduMap = mapView.getMap(); LatLng cenpt = new LatLng(28.199996, 112.982994); MapStatus.Builder builder = new MapStatus.Builder(); builder.zoom(18) .target(cenpt); baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); baiduMap.setMyLocationEnabled(true); LocationClient LocationClient = new LocationClient(this);//通过LocationClientOption设置LocationClient相关参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000);//设置locationClientOption LocationClient.setLocOption(option);//注册LocationListener监听器 MyLocationListener myLocationListener = new MyLocationListener(); LocationClient.registerLocationListener(myLocationListener);//开启地图定位图层 LocationClient.start(); } public class MyLocationListener extends BDAbstractLocationListener { @Override //location里面有经度和纬度,谷歌总部 public void onReceiveLocation(BDLocation location) { //mapView 销毁后不在处理新接收的位置 if (location == null || mapView == null) { return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(location.getDirection()).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); baiduMap.setMyLocationData(locData); } } }
7.运行效果
二、获取相册和拍照
1.导包添加依赖
在build.gradle导包
// PictureSelector basic (Necessary) implementation 'io.github.lucksiege:pictureselector:v3.0.9'// image compress library (Not necessary) implementation 'io.github.lucksiege:compress:v3.0.9'// uCrop library (Not necessary) implementation 'io.github.lucksiege:ucrop:v3.0.9'// simple camerax library (Not necessary) implementation 'io.github.lucksiege:camerax:v3.0.9' implementation 'com.github.bumptech.glide:glide:4.13.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
2.在AndroidManifest.xml中获取权限配置
<queries package="${applicationId}"> <intent> <action android:name="android.media.action.IMAGE_CAPTURE"> </action> </intent> <intent> <action android:name="android.media.action.ACTION_VIDEO_CAPTURE"> </action> </intent> </queries> android:requestLegacyExternalStorage="true" <uses-feature android:name="android.hardware.camera.any" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
3.新建一个GlideEngine类
4.运行代码
package com.hnucm.baidumap;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.Manifest;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.Toast;import com.baidu.location.BDAbstractLocationListener;import com.baidu.location.BDLocation;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.MapStatus;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.MyLocationData;import com.baidu.mapapi.model.LatLng;import com.bumptech.glide.Glide;import com.luck.picture.lib.basic.PictureSelector;import com.luck.picture.lib.config.SelectMimeType;import com.luck.picture.lib.entity.LocalMedia;import com.luck.picture.lib.interfaces.OnResultCallbackListener;import com.permissionx.guolindev.PermissionX;import com.permissionx.guolindev.callback.RequestCallback;import java.io.File;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { MapView mapView; BaiduMap baiduMap; LocationClient locationClient; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PermissionX.init(this) .permissions(Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION) .request(new RequestCallback() { @Override public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) { if (allGranted) { Toast.makeText(MainActivity.this, "All permissions are granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "These permissions are denied: $deniedList", Toast.LENGTH_LONG).show(); } } }); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //获取相册 PictureSelector.create(MainActivity.this) .openGallery(SelectMimeType.ofImage()) .setImageEngine(com.hnucm.baidumap.GlideEngine.createGlideEngine()) .forResult(new OnResultCallbackListener<LocalMedia>() { @Override public void onResult(ArrayList<LocalMedia> result) { } @Override public void onCancel() { } }); PictureSelector.create(MainActivity.this) .openGallery(SelectMimeType.ofImage()) .setImageEngine(GlideEngine.createGlideEngine()) .forResult(new OnResultCallbackListener<LocalMedia>() { @Override public void onResult(ArrayList<LocalMedia> result) { Glide.with(MainActivity.this).load(Uri.fromFile(new File(result.get(0).getRealPath()))).into(imageView); } @Override public void onCancel() { } }); } }); findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击拍照 PictureSelector.create(MainActivity.this) .openCamera(SelectMimeType.ofImage()) .forResult(new OnResultCallbackListener<LocalMedia>() { @Override public void onResult(ArrayList<LocalMedia> result) { imageView= findViewById(R.id.imageView); Glide.with(MainActivity.this).load(Uri.fromFile(new File(result.get(0).getRealPath()))).into(imageView); } @Override public void onCancel() { } }); // 点击拍照 PictureSelector.create(MainActivity.this) .openCamera(SelectMimeType.ofImage()) .forResult(new OnResultCallbackListener<LocalMedia>() { @Override public void onResult(ArrayList<LocalMedia> result) { Glide.with(MainActivity.this).load(Uri.fromFile(new File(result.get(0).getRealPath()))).into(imageView); } @Override public void onCancel() { } }); } }); }}
5.效果展示
按住shift可以移动相机,还可以从相册得到拍摄的图片
总结
好了,那么今天的学习就到这里了。友友们觉得不错的可以给个关注,点赞或者收藏哦!感谢各位友友们的支持。以下的代码希望各位大佬们自行检验哦,毕竟亲手操作让记忆更加深刻。