ken0625 score 40
2016/05/24 23:46 投稿
SurfaceViewをフラグメント上に追加したい |
surfaceViewをフラグメントのViewにプログラムから追加したいのですがsurfaceViewCreatedを呼んでくれません |
```ここに言語を入力 |
public class EditPictureFragment extends Fragment { |
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; |
private final int MP = ViewGroup.LayoutParams.MATCH_PARENT; |
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ |
View view = inflater.inflate(R.layout.layout_edit_picture, container, false); |
Uri uri = Uri.parse(getArguments().getString("uri")); |
LinearLayout linearLayout =(LinearLayout)view.findViewById(R.id.linearLayout2); |
PictureSurfaceView pictureSurfaceView = new PictureSurfaceView(getActivity(),uri); |
linearLayout.addView(pictureSurfaceView, 0, new LinearLayout.LayoutParams(WC, WC)); |
return view; |
} |
``` |
fragmentで追加しているレイアウトリソースがこれです |
```ここに言語を入力 |
<?xml version="1.0" encoding="utf-8"?> |
<ScrollView |
android:id="@+id/ScrollView2" |
android:layout_height="fill_parent" |
android:layout_width="fill_parent" |
xmlns:android="http://schemas.android.com/apk/res/android" |
> |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
android:orientation="vertical" android:layout_width="fill_parent" |
android:layout_height="wrap_content" |
android:id="@+id/linearLayout2"> |
</LinearLayout> |
</ScrollView> |
``` |
下がsurfaceViewのクラスです |
```ここに言語を入力 |
public class PictureSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{ |
Canvas canvas; |
Bitmap picture; |
SurfaceHolder s_holder; |
Float scale; |
Uri uri; |
Context context; |
public PictureSurfaceView(Context context,Uri uri) { |
super(context); |
this.context = context; |
s_holder = getHolder(); |
this.uri = uri; |
s_holder.addCallback(this); |
// deprecated setting, but required on Android versions prior to 3.0 |
} |
@Override |
public void surfaceCreated(SurfaceHolder surfaceHolder) { |
float scaleX = getWidth(); |
float scaleY = getHeight(); |
scale = scaleX > scaleY ? scaleY : scaleX; |
final BitmapFactory.Options options = new BitmapFactory.Options(); |
ContentResolver cr = context.getContentResolver(); |
options.inJustDecodeBounds = true; |
try { |
BitmapFactory.decodeStream(cr.openInputStream(uri), null, options); |
} catch (FileNotFoundException e) { |
e.printStackTrace(); |
} |
int width = getWidth(); |
int height = (int) (width * (float) options.outHeight / (float) options.outWidth); |
picture=BitMapHandler.decodeSampledBitmapFromUri(context,uri,width,height); |
Log.d("message","surfaceView = "+picture.toString()); |
} |
@Override |
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { |
} |
@Override |
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { |
} |
@Override |
public void run() { |
while(true){ |
canvas = s_holder.lockCanvas(); |
draw(); |
s_holder.unlockCanvasAndPost(canvas); |
canvas.translate(getWidth()/2*scale, getHeight()/2*scale); // 画面の中央になるように移動させる |
canvas.scale(scale, scale); |
try{ |
Thread.sleep(100);//次の描画まで100msecの間隔を空ける |
//(フレームレートを厳守するのであれば,sleepさせるのではなく,描画処理を一定間隔で呼び出すようにする) |
}catch(InterruptedException e){ |
e.printStackTrace(); |
} |
} |
} |
public void draw(){ |
Paint paint = new Paint(); |
canvas.drawBitmap(picture,0,0,paint); |
} |
} |
``` |
``` |
何が間違っているのでしょうか...お願いします |