我有一本漫畫書佈局的.BMP圖像。目前我的代碼是這樣工作的。如果我右鍵單擊並按住鼠標按鈕,我可以在漫畫書頁面的其中一個框架周圍繪製一個選框類型框。當我釋放按鈕時,它將放大到該框架。但它的瞬間。我希望它具有動畫效果。數學慢的圖像變焦
而不是去和PicRect的值設置爲「終值」
PicRect.Left
PicRect.right
PicRect.top
PicRect.bottom
如以下代碼因此,我需要一種方法來慢慢那裏,有些種類while循環,設置這些值一點點在一個時間,直到,直到它得到了「終值」可是我不是如何數學工作確保100%。我的while循環中的任何一個都不會做任何事情,只是放大得太遠而已。這是程序。
procedure TZImage.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
var coef:Double;
t:integer;
begin
if FMouse=mNone then Exit;
if x>ShowRect.Right then x:=ShowRect.Right;
if y>ShowRect.Bottom then y:=ShowRect.Bottom;
if FMouse=mZoom then begin //calculate new PicRect
t:=startx;
startx:=Min(startx,x);
x:=Max(t,x);
t:=starty;
starty:=Min(starty,y);
y:=Max(t,y);
FMouse:=mNone;
MouseCapture:=False;
//enable the following if you want to zoom-out by dragging in the opposite direction}
{ if Startx>x then begin
DblClick;
Exit;
end;}
if Abs(x-startx)<5 then Exit;
if (x - startx < y - starty) then
begin
while (x - startx < y - starty) do
begin
x := x + 100;
startx := startx - 100;
end;
end
else if (x - startx > y - starty) then
begin
while (x - startx > y - starty) do
begin
y := y + 100;
starty := starty - 100;
end;
end;
//This is were it sets the zoom info. This is were
//I have to change to slowly get the PICRECT.Left/right/top/bottom
if (PicRect.Right=PicRect.Left)
then
coef := 100000
else
coef:=ShowRect.Right/(PicRect.Right-PicRect.Left);
PicRect.Left:=Round(PicRect.Left+startx/coef);
PicRect.Right:=PicRect.Left+Round((x-startx)/coef);
if (PicRect.Bottom=PicRect.Top)
then
coef := 100000
else
coef:=ShowRect.Bottom/(PicRect.Bottom-PicRect.Top);
PicRect.Top:=Round(PicRect.Top+starty/coef);
PicRect.Bottom:=PicRect.Top+Round((y-starty)/coef);
end;
if FMouse=mDrag then begin
FMouse:=mNone;
Canvas.Pen.Mode:=pmCopy;
Screen.Cursor:=crDefault;
end;
Invalidate;
end;
我相信這可以在上面的代碼中完成。但也想補充一點,這有助於。
type
TZImage = class(TGraphicControl)
private
FBitmap : TBitmap;
PicRect : TRect;
ShowRect : TRect;
FShowBorder : boolean;
FBorderWidth : integer;
FForceRepaint : boolean;
FMouse : (mNone, mDrag, mZoom);
FProportional : boolean;
FDblClkEnable : boolean;
startx, starty,
oldx, oldy : integer;
感謝您的任何幫助,使其工作。
問題是它會拉伸,增加的循環現在停止拉伸。如果註釋掉了x - start == y - starty,它們的作用相同。我會試着用while循環看看它是如何工作的,謝謝你。 Iam不知道COEF甚至想要做什麼:(我計算了一個while循環會取代你稱之爲有點混淆的代碼段......因爲它上面的while循環是去除了伸展。 –
將接受,因爲這有助於我獲得每一個工作的效果! –
What'd be _awesome_是,如果你用_fixed_代碼添加一個答案 - 別人試圖做一些圖像縮放動畫可能非常想知道什麼特別需要改變 – sarnold