我想用AnimatedGifEncoder创建一个gif文件,其工作成功创建了我的gif,但是我无法更改帧和其他设置(如质量)之间的延迟。
还有其他人在android上使用此类吗?
这是我的代码:
a=[1,2,3,4,5,6,7]
b=[8,9,10,11,12,13,14]
c=[{i:j} for i in a for j in b]
print(c)
这是我的代码:
[{1: 8}, {1: 9}, {1: 10}, {1: 11}, {1: 12}, {1: 13}, {1: 14}, {2: 8}, {2: 9}, {2: 10}, {2: 11}, {2: 12}, {2: 13}, {2: 14}, {3: 8}, {3: 9}, {3: 10}, {3: 11}, {3: 12}, {3: 13}, {3: 14}, {4: 8}, {4: 9}, {4: 10}, {4: 11}, {4: 12}, {4: 13}, {4: 14}, {5: 8}, {5: 9}, {5: 10}, {5: 11}, {5: 12}, {5: 13}, {5: 14}, {6: 8}, {6: 9}, {6: 10}, {6: 11}, {6: 12}, {6: 13}, {6: 14}, {7: 8}, {7: 9}, {7: 10}, {7: 11}, {7: 12}, {7: 13}, {7: 14}]
}
答案 0 :(得分:0)
我建议您使用此库 Android NDK GIF Library ,它比AnimatedGifEncoder
更好:
这是如何生成GIF文件并将其保存到图库的完整示例:
@SuppressLint("StaticFieldLeak")
public class GenerateSaveGIF extends AsyncTask<Void, Integer, String> {
Context mContext;
String imageFileName = "APP_" + System.currentTimeMillis() + ".gif";
ArrayList<BitmapGIFUtil> arrayListBitmaps = new ArrayList<>();
boolean isSavingBitmapsFinished = false;
GenerateSaveGIF(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Preparing;
// Display ProgressDialog here
}
@SuppressLint("WrongThread")
@Override
protected String doInBackground(Void... voids) {
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/APP_FOLDER");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
FileOutputStream outStream = null;
try {
generateGIF(getArrayBitmaps(), savedImagePath);
} catch (Exception e) {
e.printStackTrace();
}
galleryAddPic(savedImagePath);
}
return savedImagePath;
}
ArrayList<BitmapGIFUtil> getArrayBitmaps() {
for (int i = 0; i < mDrawableGif.getNumberOfFrames(); ++i) {
int GIFDelay = mDrawableGif.getFrameDuration(i); //CHANGE IT IF YOU WANT
try {
Thread.sleep(GIFDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
//replace createEachFrame() with your method getBitmapFromResource();
arrayListBitmaps.add(new BitmapGIFUtil(createEachFrame(), GIFDelay));
}
isSavingBitmapsFinished = true;
return arrayListBitmaps;
}
void generateGIF(ArrayList<BitmapGIFUtil> arrayList, String path) {
GifEncoder gifEncoder = new GifEncoder();
try {
gifEncoder.init(arrayList.get(0).getBitmap().getWidth(), arrayList.get(0).getBitmap().getHeight(), path, GifEncoder.EncodingType.ENCODING_TYPE_SIMPLE_FAST);
for (int i = 0; i < arrayList.size(); i++) {
gifEncoder.encodeFrame(arrayList.get(i).getBitmap(), arrayList.get(i).getDelay());
}
gifEncoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(this, getResources().getString(R.string.gif_saved), Toast.LENGTH_SHORT).show();
}
}
通知画廊:
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
BitmapGIFUtil类:
public class BitmapGIFUtil {
private Bitmap bitmap;
private int Delay;
public BitmapGIFUtil() {
}
public BitmapGIFUtil(Bitmap bitmap, int delay) {
this.bitmap = bitmap;
Delay = delay;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getDelay() {
return Delay;
}
public void setDelay(int delay) {
Delay = delay;
}
}