澳门太阳娱乐集团官网-太阳集团太阳娱乐登录

Android签名
分类:脚本专栏

叁个简约的勤学苦练,手写具名后,能够清空,保存,然后再相册实行查看

原来的小说是官方文书档案的Generating Signed APK](https://link.jianshu.com?t=%5Bhttps://facebook.github.io/react-native/docs/signed-apk-android.html)]()))

图片 1

图片 2轻便易行具名板

1.生成签字用的key

在项目根目录下运作

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

这么便会生成名字为my-release-key.keystore的keystore。一些值得注意的点:

  • key的保藏期(validity)为一千0天
  • 别名(alias)my-key-alias在等会儿给app具名时要用到。

具名和校验的流程

有5个知识点,须求留意:

2.设置gradle变量

①将刚刚生成的my-release-key.keystore文本放到你的React Native项目中的android/app目录下
②编辑android/gradle.properties,加多以下内容:

MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=*****
MYAPP_RELEASE_KEY_PASSWORD=*****

将*****轮换为刚刚设置的keystore和key的密码

多路子打包

  1. SignatureViewonTouchEvent()方法中,利用mPath.quadTo()情势,使绘制路线变得狡猾
  2. SignatureViewsave()方法中,将View中的内容保留到七个Bitmap
  3. SignatureViewcloseStream()艺术中,全体的读写流都完毕了Closeable接口,能够用来关闭流
  4. SignatureViewclear()方法中,利用PorterDuff.Mode.CLEARCanvas中绘制的源委清空
  5. MainActivity中,图片保存到地点后,供给向系统一发布送多个广播,公告相册更新

3.在app的gradle设置中增加签字设置

编写制定文件android/app/build.gradle

...
android {
  ...
    defaultConfig { ... }
    signingConfigs {
        release {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }
}
...

可选项:开启Proguard减小apk的大小

def enableProguardInReleaseBuilds = true

先通过mCanvas利用mPaint将绘制的路径保存进了mBitmap中,再将mBitmapcanvas绘制出来

3.生成release版本的APK

$ cd android && ./gradlew assembleRelease

接下来你就足以在android/app/build/outputs/apk/app-release.apk中找到变化的APK

public class SignatureView extends View { private Paint mPaint; private Path mPath; private Canvas mCanvas; private Bitmap mBitmap; private float mLastX, mLastY;//上次的坐标 public SignatureView(Context context, AttributeSet attrs) { super(context, attrs); init(); } /*** * 初始化 */ private void init() { //关闭硬件加速 setLayerType(View.LAYER_TYPE_SOFTWARE, null); //画笔 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); mPaint.setStrokeWidth; mPaint.setColor(Color.parseColor("#FF4081")); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND);//使画笔更加圆润 mPaint.setStrokeCap(Paint.Cap.ROUND);//同上 //路径 mPath = new Path(); //保存签名的画布 post(new Runnable() {//拿到控件的宽和高 @Override public void run() { mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); mCanvas = new Canvas; } }); } /** * 绘制 */ @Override protected void onDraw(Canvas canvas) { super.onDraw; if (mBitmap != null) { canvas.drawColor(Color.WHITE);//绘制背景白色 drawSignaturePath(); //将路径绘制在mBitmap上 canvas.drawBitmap(mBitmap, 0, 0, null);//将mBitmap绘制在canvas上 } } /** * 清空绘制内容 */ public void clear() { mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); mCanvas.drawPaint; mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); mPath.reset(); invalidate(); } /** * 保存到指定的文件夹中 */ public boolean save(String filePath) { if (mBitmap != null && mLastY != 0f) {//没有绘制,就不保存 //从View中得到Bitmap setDrawingCacheEnabled; buildDrawingCache; Bitmap bitmap = getDrawingCache; //保存图片 File file = new File; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream; if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) { fileOutputStream.flush(); return true; } } catch (java.io.IOException e) { e.printStackTrace(); } finally { closeStream(fileOutputStream); } } return false; } /** * 关闭流 */ private void closeStream(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 绘制签名 */ private void drawSignaturePath() { mCanvas.drawPath(mPath, mPaint); } /** * 触摸事件 触摸绘制 */ @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); float x = event.getX(); float y = event.getY(); switch  { case MotionEvent.ACTION_DOWN: mLastX = x; mLastY = y; mPath.moveTo(mLastX, mLastY); break; case MotionEvent.ACTION_MOVE: float dx = Math.abs(x - mLastX); float dy = Math.abs(y - mLastY); if (dx >= 3 || dy >= 3) {//绘制的最小距离 3px //利用二阶贝塞尔曲线,使绘制路径更加圆滑 mPath.quadTo(mLastX, mLastY, (mLastX + x) / 2, (mLastY + y) / 2); } mLastX = x; mLastY = y; break; case MotionEvent.ACTION_UP: mPath.reset(); break; } invalidate(); return true; } /** * 测量 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int wSpecMode = MeasureSpec.getMode(widthMeasureSpec); int wSpecSize = MeasureSpec.getSize(widthMeasureSpec); int hSpecMode = MeasureSpec.getMode(heightMeasureSpec); int hSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (wSpecMode == MeasureSpec.EXACTLY && hSpecMode == MeasureSpec.EXACTLY) { setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } else if (wSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, hSpecSize); } else if (hSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(wSpecSize, 200); } }}

4.设置测量试验

$ cd android && ./gradlew installRelease

重在的地点都有注释

在图片保存成功后,将图纸的路线音信应用用广播发送给系统,公告相册更新

public class MainActivity extends AppCompatActivity { private Button bt_clear,bt_save; private SignatureView sv; private final String APP_DIR = "com.signature.view"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } /** * 初始化 */ private void init() { final String dir = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+APP_DIR+File.separator; final File fileDir = new File; if (!fileDir.exists{ fileDir.mkdirs(); } bt_clear =  findViewById(R.id.bt_clear_main_activity); sv = (SignatureView) findViewById(R.id.sv_main_activity); bt_clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick { sv.clear; bt_save =  findViewById(R.id.bt_save_main_activity); bt_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick { save; } }); } /** * 保存图片 */ private void save(File fileDir) { final String filePath = getFilePath; if (sv.save){//保存成功 //发送广播 通知系统相册更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File); intent.setData; sendBroadcast; Toast.makeText(MainActivity.this, "保存成功!!!", Toast.LENGTH_SHORT).show(); } } /** * 得到图片的路径 以及图片的名字 */ private String getFilePath(File fileDir) { SimpleDateFormat simpleDateFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance(); final String fileName = simpleDateFormat.format(new Date+".png"; Log.e("filename","---"+fileName); File file = new File(fileDir,fileName); return file.getAbsolutePath(); }}

DateFormat.getDateTimeInstance()这些措施很有益于的拿到手提式有线电话机安装的时光显示格式

布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:andro android: android:layout_width="match_parent" android:layout_height="match_parent"> <com.szlk.signatureview.view.SignatureView android: android:layout_width="match_parent" android:layout_height="400dp" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/sv_main_activity" android:layout_marginStart="50dp" android:text="清空"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/sv_main_activity" android:layout_alignParentEnd="true" android:layout_marginEnd="50dp" android:text="保存" /></RelativeLayout>

布局很简陋

一言九鼎是演练一下,复习这两天一段学习到的片段学问

有多少个十三分牛的签名板,能够学习一下张明云南大学神的Android手写优化-更为平滑的签定效果落到实处

共勉 : )

本文由澳门太阳娱乐集团官网发布于脚本专栏,转载请注明出处:Android签名

上一篇:自定义View系列 下一篇:没有了
猜你喜欢
热门排行
精彩图文