我在Android中制作程序动画,假设只拍摄720x720图像,转换为2d颜色数组,然后逐个绘制每个像素。我的代码的问题是它只需要左上象限并将其显示为整个图像,我无法弄清楚如何修复它。
这里的代码,源图像和结果:
angular.forEach(data.colors, function(value, key) {
$scope.colorList.push(value);
});
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawPixels(this, 720, 720));
}
}
public class DrawPixels extends View {
public int[][] image_array;
Bitmap frame;
Bitmap photo;
Canvas frameDrawer;
Rect bounds;
Paint lp;
int width , height;
int x = 0;
int y = 0;
int radius = 3;
public DrawPixels(Context context , int width, int height) {
super(context);
photo = BitmapFactory.decodeResource(context.getResources(), R.drawable.marilyn2);
image_array = imageTo2dArray(photo);
this.width = width;
this.height = height;
frame = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
frameDrawer = new Canvas(frame);
bounds = new Rect(0 , 0, width,height);
lp = new Paint();
lp.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
if (x < 720 && y < 720) {
lp.setColor(image_array[x][y]);
frameDrawer.drawCircle(x, y, radius, lp);
}
canvas.drawBitmap(frame, null, bounds , null);
//sampling every 5th pixel
if (x < 720){
if(y < 720){
y += 5;
}else{
y = 0;
x += 5;
}
invalidate();
}
}
public int[][] imageTo2dArray(Bitmap bm){
int[][] arr = new int[720][720];
int p, a, r, g, b, avg;
for(int i = 0; i < 720; i++) {
for (int j = 0; j < 720; j++) {
arr[i][j] = bm.getPixel(i, j);
}
}
return arr;
}
}
答案 0 :(得分:1)
最可能的原因是BitmapFactory.decodeResource缩放位图,使其不再是720x720而是1440x1440。
请参阅:BitmapFactory.decodeResource Bitmap not original size in pixels