All Projects → lchad → Gifflen-Android

lchad / Gifflen-Android

Licence: other
Android上合成gif图片.

Programming Languages

C++
36643 projects - #6 most used programming language
java
68154 projects - #9 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to Gifflen-Android

sail
The missing small and fast image decoding library for humans (not for machines) ⛵ https://sail.software
Stars: ✭ 206 (+281.48%)
Mutual labels:  gif
gifbar
🦄 Find Awesome Gif's right in your Menu Bar
Stars: ✭ 37 (-31.48%)
Mutual labels:  gif
bitmap-sdf
Calculate SDF for image/bitmap/bw data
Stars: ✭ 25 (-53.7%)
Mutual labels:  bitmap
GifReversingBot
A Reddit bot that reverses gifs
Stars: ✭ 38 (-29.63%)
Mutual labels:  gif
FastBitmap
A fast C# Bitmap wrapping layer
Stars: ✭ 86 (+59.26%)
Mutual labels:  bitmap
AvatarImageGenerator
Android library to generate image avatar from the first letter of a username. Letter avatar like Gmail Android best practice
Stars: ✭ 61 (+12.96%)
Mutual labels:  bitmap
StbSharp
C# port of the famous C framework
Stars: ✭ 62 (+14.81%)
Mutual labels:  gif
makesweet
Put pictures into animations from the command line.
Stars: ✭ 18 (-66.67%)
Mutual labels:  gif
BMPtoArray
Python script to convert a bitmap to an Arduino prog_mem array for outputting grayscale images to OLEDs.
Stars: ✭ 25 (-53.7%)
Mutual labels:  bitmap
congif
convert script(1) output to GIF
Stars: ✭ 52 (-3.7%)
Mutual labels:  gif
GiphyImageResultsPreview
Library for GIF results, preview, play, share everything at one place!
Stars: ✭ 28 (-48.15%)
Mutual labels:  gif
whatisnewdialog
An Android library for displaying a dialog where it presents new features in the app.
Stars: ✭ 22 (-59.26%)
Mutual labels:  gif
Among-Us-Dumpy-Gif-Maker
A tool to make dumpy among us GIFS
Stars: ✭ 537 (+894.44%)
Mutual labels:  gif
togglific
Do you find web animations distracting? Togglific provides a distraction-free web experience!
Stars: ✭ 17 (-68.52%)
Mutual labels:  gif
piskel
A simple web-based tool for Spriting and Pixel art.
Stars: ✭ 9,109 (+16768.52%)
Mutual labels:  gif
gif-player
Control your animated GIFs
Stars: ✭ 120 (+122.22%)
Mutual labels:  gif
SSSwiftUIGIFView
SSSwiftUIGIFView is a custom controller which helps to load GIF in SwiftUI.
Stars: ✭ 48 (-11.11%)
Mutual labels:  gif
gifken
JavaScript library that can reverse and split animated GIFs
Stars: ✭ 25 (-53.7%)
Mutual labels:  gif
stumpy gif
Write GIF images in pure Crystal
Stars: ✭ 18 (-66.67%)
Mutual labels:  gif
Kamel
Kotlin asynchronous media loading and caching library for Compose.
Stars: ✭ 138 (+155.56%)
Mutual labels:  gif

Gifflen-Android

在Android平台通过静态图片合成Gif动态图片.

示例Apk程序

点我下载

一脸懵逼.gif 是如何产生的?

由于Android平台对Gif的支持不是很好,没有现成的Java Api可以用,所以我们需要借助NDK作为桥梁,通过C++语言实现对32-bit ARGB图片进行 Color Quantization,转化成Gif动态图(256色域).这个项目的色彩转换算法是基于 Gifflen @Bitmap color reduction and GIF encoding 的.

通过对Gifflen的C++源码进行一些定制和修改,然后编译出native library(即 .so 文件),然后打包到 APK 中.我们可以很容易的在Android系统上创建一个Gif动图. 本项目的NDK部分代码是采用CMake的方式进行构建的.Android Studio2.2版本之后已经对NDK编程有了很好的支持.


使用方法

1.添加动态链接库

在此项目根目录下的so目录有我编译好的动态链接库文件,包含arm-v8a, armaib, armabi-v7a, mips, mips64, x86, x86-64 等7个平台,你可以根据自己的需要添加对应的native library到项目中.

2.添加 Gifflen 工具类

应用层的所有接口都在Gifflen.java这个类中,你要做的就是在自己项目的app/main/java路径下创建com/lchad/gifflen文件夹,然后把Gifflen.java复制到这个路径下,注意,请一定要把Gifflen.java放到以上指定位置(即your project name/app/main/java/com/lchad/gifflen)下,否则会在运行时报错,找不到jni方法.这一点需格外注意.

3.配置读写存储的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

如果是Android6.0及以上需注意动态申请权限.

 ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_PERMISSIONS);

**4.初始化工具类 Gifflen **

Gifflen mGiffle = new Gifflen.Builder()
                        .color(mColor)	//色域范围是2~256,且必须是2的整数次幂.
                        .delay(mDelayTime) //每相邻两帧之间播放的时间间隔.
                        .quality(mQuality) //色彩量化时的quality值.
                        .width(500)	//生成Gif文件的宽度(像素).
                        .height(500)	//生成Gif文件的高度(像素).
                        .listener(new Gifflen.OnEncodeFinishListener() {  //创建完毕的回调
                             @Override
                             public void onEncodeFinish(String path) {
                                 Toast.makeText(MainActivity.this, "已保存gif到" + path, Toast.LENGTH_LONG).show();
                                 try {
                                     GifDrawable gifFromPath = new GifDrawable(mStorePath);
                                     mGifImageView.setImageDrawable(gifFromPath);
                                 } catch (IOException e) {
                                     e.printStackTrace();
                                 }
                             }
                         })
                        .build();

5.开始创建 Gif 图片

  • 从File列表创建
	List<File> files = getFileList();
        mGiffle.encode(320, 320, "target path", files);
        mGiffle.encode("target path", files); 
  • 从 Uri 列表创建
        List<Uri> uris = getUriList();
        mGiffle.encode(context, 500, 500, uris);
        mGiffle.encode(context, "target path", uris);
  • 从 TypeArray 创建
	 TypeArra mDrawableList = getResources().obtainTypedArray(R.array.source);
	 mGiffle.encode(MainActivity.this, "target path", 500, 500, mDrawableList);
	 mGiffle.encode(MainActivity.this, "target path", mDrawableList);
  • 从 Bitmap 数组创建
        Bitmap[] bitmaps = getBitmaps();
        mGiffle.encode("target path", 500, 500, bitmaps);
        mGiffle.encode("target path", bitmaps);
	//注意:此时要考虑Bitmap的大小以及个数,否则可能会造成OOM.
  • 从drawable id 数组创建
	int[] drawableIds = new int[]{
                R.drawable.mengbi1,
                R.drawable.mengbi2,
                R.drawable.mengbi3};
        mGiffle.encode(context, "target path", 500, 500, drawableIds);
        mGiffle.encode(context, "target path", drawableIds);

以上五种创建方式都支持重载(本质上都是对Bitmap进行操作),宽度和高度在encode()的时候都可以缺省,这时会使用创建Gifflen时传入的值,如果创建Gifflen时仍然没有传值,则会使用一个默认的值320. encode()属于耗时操作,建议将其执行放到子线程内部,以免阻塞UI线程.

我开了一个使用GIfflen-Android的示例项目: GIfflen-Android-Config-Sample,可以做一个参考.

具体的实现原理以及细节可以移步我的博客: lchad

License

Copyright 2017 lchad
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].