|
| 1 | +package com.coorchice.supertextview; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.text.TextUtils; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.EditText; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import com.coorchice.library.SuperTextView; |
| 13 | +import com.coorchice.library.gifdecoder.GifDrawable; |
| 14 | +import com.coorchice.library.utils.STVUtils; |
| 15 | +import com.coorchice.library.utils.ThreadPool; |
| 16 | + |
| 17 | +public class GifActivity extends Activity { |
| 18 | + |
| 19 | + private SuperTextView stvGifScreen; |
| 20 | + private SuperTextView stvPre; |
| 21 | + private SuperTextView stvPlay; |
| 22 | + private SuperTextView stvNext; |
| 23 | + private SuperTextView stvStrict; |
| 24 | + private SuperTextView stvScaleType; |
| 25 | + private EditText etFrameDuration; |
| 26 | + private SuperTextView stvFrameDuration; |
| 27 | + private EditText etGotoFrame; |
| 28 | + private SuperTextView stvGotoFrame; |
| 29 | + private EditText etGetFrame; |
| 30 | + private SuperTextView stvGetFrame; |
| 31 | + private SuperTextView stvShowFrame; |
| 32 | + private SuperTextView tvInfo; |
| 33 | + |
| 34 | + private long gifMemory = 0; |
| 35 | + private long lastDrawTime = System.currentTimeMillis(); |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void onCreate(Bundle savedInstanceState) { |
| 39 | + super.onCreate(savedInstanceState); |
| 40 | + setContentView(R.layout.activity_gif); |
| 41 | + initView(); |
| 42 | + addListener(); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + private void initView() { |
| 47 | + stvGifScreen = (SuperTextView) findViewById(R.id.stv_gif_screen); |
| 48 | + stvPre = (SuperTextView) findViewById(R.id.stv_pre); |
| 49 | + stvPlay = (SuperTextView) findViewById(R.id.stv_play); |
| 50 | + stvNext = (SuperTextView) findViewById(R.id.stv_next); |
| 51 | + stvStrict = (SuperTextView) findViewById(R.id.stv_strict); |
| 52 | + stvScaleType = (SuperTextView) findViewById(R.id.stv_scale_type); |
| 53 | + etFrameDuration = (EditText) findViewById(R.id.et_frame_duration); |
| 54 | + stvFrameDuration = (SuperTextView) findViewById(R.id.stv_frame_duration); |
| 55 | + etGotoFrame = (EditText) findViewById(R.id.et_goto_frame); |
| 56 | + stvGotoFrame = (SuperTextView) findViewById(R.id.stv_goto_frame); |
| 57 | + etGetFrame = (EditText) findViewById(R.id.et_get_frame); |
| 58 | + stvGetFrame = (SuperTextView) findViewById(R.id.stv_get_frame); |
| 59 | + stvShowFrame = (SuperTextView) findViewById(R.id.stv_show_frame); |
| 60 | + tvInfo = (SuperTextView) findViewById(R.id.tv_info); |
| 61 | + |
| 62 | + stvPlay.setSelected(true); |
| 63 | + ThreadPool.run(new Runnable() { |
| 64 | + @Override |
| 65 | + public void run() { |
| 66 | + byte[] resBytes = STVUtils.getResBytes(GifActivity.this, R.drawable.gif_m_7); |
| 67 | + gifMemory = resBytes.length / 1024; |
| 68 | + final GifDrawable drawable = GifDrawable.createDrawable(resBytes); |
| 69 | + runOnUiThread(new Runnable() { |
| 70 | + @Override |
| 71 | + public void run() { |
| 72 | + stvGifScreen.setDrawable(drawable); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private void addListener() { |
| 80 | + stvGifScreen.setAutoAdjust(true); |
| 81 | + stvGifScreen.addAdjuster(new SuperTextView.Adjuster() { |
| 82 | + @Override |
| 83 | + protected void adjust(SuperTextView v, Canvas canvas) { |
| 84 | + if (stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 85 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 86 | + StringBuilder sb = new StringBuilder() |
| 87 | + .append("gif memory : ").append(gifMemory / 1000).append("MB").append(gifMemory % 1000).append("KB\n") |
| 88 | + .append("ptr : ").append(gifDrawable.getPtr()).append("\n") |
| 89 | + .append("width : ").append(gifDrawable.getWidth()).append("\n") |
| 90 | + .append("height : ").append(gifDrawable.getHeight()).append("\n") |
| 91 | + .append("frame count : ").append(gifDrawable.getFrameCount()).append("\n") |
| 92 | + .append("frame duration : ").append(gifDrawable.getFrameDuration()).append("ms\n") |
| 93 | +// .append("rel frame duration : ").append(System.currentTimeMillis() - lastDrawTime).append("ms\n") |
| 94 | + .append("enable strict : ").append(gifDrawable.isStrict()).append("\n") |
| 95 | + .append("current frame : ").append(gifDrawable.getCurrentFrame()); |
| 96 | + if (!(tvInfo.getText() != null && tvInfo.getText().toString().equals(sb.toString()))) { |
| 97 | + tvInfo.setText(sb); |
| 98 | + } |
| 99 | + lastDrawTime = System.currentTimeMillis(); |
| 100 | + } |
| 101 | + } |
| 102 | + }.setOpportunity(SuperTextView.Adjuster.Opportunity.AT_LAST)); |
| 103 | + |
| 104 | + stvScaleType.setOnClickListener(new View.OnClickListener() { |
| 105 | + @Override |
| 106 | + public void onClick(View v) { |
| 107 | + switch (stvGifScreen.getScaleType()) { |
| 108 | + case FIT_XY: |
| 109 | + stvGifScreen.setScaleType(SuperTextView.ScaleType.FIT_CENTER); |
| 110 | + Toast.makeText(GifActivity.this, "ScaleType:FIT_CENTER", Toast.LENGTH_SHORT).show(); |
| 111 | + break; |
| 112 | + case FIT_CENTER: |
| 113 | + stvGifScreen.setScaleType(SuperTextView.ScaleType.CENTER); |
| 114 | + Toast.makeText(GifActivity.this, "ScaleType:CENTER", Toast.LENGTH_SHORT).show(); |
| 115 | + break; |
| 116 | + case CENTER: |
| 117 | + stvGifScreen.setScaleType(SuperTextView.ScaleType.FIT_XY); |
| 118 | + Toast.makeText(GifActivity.this, "ScaleType:FIT_XY", Toast.LENGTH_SHORT).show(); |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + stvPlay.setOnClickListener(new View.OnClickListener() { |
| 125 | + @Override |
| 126 | + public void onClick(View v) { |
| 127 | + stvPlay.setSelected(!stvPlay.isSelected()); |
| 128 | + stvPlay.setDrawable(stvPlay.isSelected() ? R.drawable.icon_stop : R.drawable.icon_play); |
| 129 | + if (stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 130 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 131 | + if (stvPlay.isSelected()) { |
| 132 | + gifDrawable.play(); |
| 133 | + } else { |
| 134 | + gifDrawable.stop(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + stvPre.setOnClickListener(new View.OnClickListener() { |
| 141 | + @Override |
| 142 | + public void onClick(View v) { |
| 143 | + if (stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 144 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 145 | + int pre = gifDrawable.getCurrentFrame() - 1; |
| 146 | + if (pre < 0) { |
| 147 | + pre = 0; |
| 148 | + } |
| 149 | + gifDrawable.gotoFrame(pre); |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + stvNext.setOnClickListener(new View.OnClickListener() { |
| 155 | + @Override |
| 156 | + public void onClick(View v) { |
| 157 | + if (stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 158 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 159 | + int frameCount = gifDrawable.getFrameCount(); |
| 160 | + int next = gifDrawable.getCurrentFrame() + 1; |
| 161 | + if (next >= frameCount) { |
| 162 | + next = frameCount - 1; |
| 163 | + } |
| 164 | + gifDrawable.gotoFrame(next); |
| 165 | + } |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + stvStrict.setOnClickListener(new View.OnClickListener() { |
| 170 | + @Override |
| 171 | + public void onClick(View v) { |
| 172 | + if (stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 173 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 174 | + boolean strict = !gifDrawable.isStrict(); |
| 175 | + stvStrict.setDrawable(strict ? R.drawable.icon_selected : R.drawable.icon_unselected); |
| 176 | + gifDrawable.setStrict(strict); |
| 177 | + } |
| 178 | + } |
| 179 | + }); |
| 180 | + |
| 181 | + stvFrameDuration.setOnClickListener(new View.OnClickListener() { |
| 182 | + @Override |
| 183 | + public void onClick(View v) { |
| 184 | + String num = etFrameDuration.getText().toString(); |
| 185 | + if (!TextUtils.isEmpty(num) && stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 186 | + int i = Integer.valueOf(num); |
| 187 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 188 | + gifDrawable.setFrameDuration(i); |
| 189 | + } |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + stvGotoFrame.setOnClickListener(new View.OnClickListener() { |
| 194 | + @Override |
| 195 | + public void onClick(View v) { |
| 196 | + String num = etGotoFrame.getText().toString(); |
| 197 | + if (!TextUtils.isEmpty(num) && stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 198 | + int i = Integer.valueOf(num); |
| 199 | + GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 200 | + int frameCount = gifDrawable.getFrameCount(); |
| 201 | + if (i >= frameCount) { |
| 202 | + i = frameCount - 1; |
| 203 | + } else if (i < 0) { |
| 204 | + i = 0; |
| 205 | + } |
| 206 | + gifDrawable.gotoFrame(i); |
| 207 | + } |
| 208 | + } |
| 209 | + }); |
| 210 | + |
| 211 | + stvGetFrame.setOnClickListener(new View.OnClickListener() { |
| 212 | + @Override |
| 213 | + public void onClick(View v) { |
| 214 | + String num = etGetFrame.getText().toString(); |
| 215 | + if (!TextUtils.isEmpty(num) && stvGifScreen.getDrawable() instanceof GifDrawable) { |
| 216 | + int i = Integer.valueOf(num); |
| 217 | + final GifDrawable gifDrawable = (GifDrawable) stvGifScreen.getDrawable(); |
| 218 | + int frameCount = gifDrawable.getFrameCount(); |
| 219 | + if (i >= frameCount) { |
| 220 | + i = frameCount - 1; |
| 221 | + } else if (i < 0) { |
| 222 | + i = 0; |
| 223 | + } |
| 224 | + if (gifDrawable.isStrict()) { |
| 225 | + final int finalPosition = i; |
| 226 | + ThreadPool.run(new Runnable() { |
| 227 | + @Override |
| 228 | + public void run() { |
| 229 | + final Bitmap frame = gifDrawable.getFrame(finalPosition); |
| 230 | + runOnUiThread(new Runnable() { |
| 231 | + @Override |
| 232 | + public void run() { |
| 233 | + stvShowFrame.setDrawable(frame); |
| 234 | + stvShowFrame.setVisibility(View.VISIBLE); |
| 235 | + } |
| 236 | + }); |
| 237 | + |
| 238 | + } |
| 239 | + }); |
| 240 | + } else { |
| 241 | + Bitmap frame = gifDrawable.getFrame(i); |
| 242 | + stvShowFrame.setDrawable(frame); |
| 243 | + stvShowFrame.setVisibility(View.VISIBLE); |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + }); |
| 248 | + |
| 249 | + stvShowFrame.setOnClickListener(new View.OnClickListener() { |
| 250 | + @Override |
| 251 | + public void onClick(View v) { |
| 252 | + stvShowFrame.setVisibility(View.GONE); |
| 253 | + } |
| 254 | + }); |
| 255 | + } |
| 256 | +} |
0 commit comments