
My XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >
<LinearLayout
android:id="@+id/leftLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="@+id/firstProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@android:color/black" />
<LinearLayout
android:id="@+id/secondProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="3dp"
android:layout_height="fill_parent"
android:background="@android:color/black" />
<LinearLayout
android:id="@+id/rightLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="@+id/thirdProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@android:color/black" />
<LinearLayout
android:id="@+id/fourthProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My View Class :
public class SpellViewFirst extends View {
private StartActivity studentActivity;
private int screenWidth;
private int screenHeight;
private Bitmap imageBitmap;
private ArrayList<Character> charList;
List<SpellObjects> spellObjectsList;
private String word;
int placeHolderHeight;
private ArrayList<Integer> dragable_id_object_list;
private Bitmap placeHolderBitmap;
Point placeHolderPoints;
Point alphabetPoints;
private int indexOfCurrentObject;
private int offsetX;
private int offsetY;
public SpellViewFirst(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
studentActivity = (StartActivity)context;
}
@Override
protected void onSizeChanged(int width, int height, int oldwidth, int oldheight) {
// TODO Auto-generated method stub
super.onSizeChanged(width, height, oldwidth, oldheight);
screenWidth = width;
screenHeight = height;
assignCoordinatesToImages();
}
private void assignCoordinatesToImages() {
// TODO Auto-generated method stub
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/resources/images/spelling/ques.png");
dragable_id_object_list=new ArrayList<Integer>() ;
spellObjectsList=new ArrayList<SpellObjects>();
SpellObjects curentObjects;
placeHolderHeight=bm.getHeight();
word=studentActivity.correctWordsArray[0];
imageBitmap=BitmapFactory.decodeFile("/mnt/sdcard/resources/images/spelling/"+word+".png");
placeHolderBitmap=Bitmap.createScaledBitmap(bm, screenWidth/word.length(), screenWidth/word.length(),true);
charList=new ArrayList<Character>();
placeHolderPoints=new Point();
for (int i = 0; i < word.length(); i++) {
charList.add(word.charAt(i));
}
for (int i = 0; i < charList.size(); i++) {
placeHolderPoints.y=screenHeight/2;
curentObjects=new SpellObjects(placeHolderPoints, placeHolderBitmap, -1,charList.get(i), null, placeHolderBitmap.getHeight(), screenWidth/word.length(), true);
placeHolderPoints.x=placeHolderPoints.x+screenWidth/word.length();
spellObjectsList.add(curentObjects);
}
Collections.shuffle(charList);
alphabetPoints=new Point();
alphabetPoints.x=imageBitmap.getWidth();
for (int i = 0; i < charList.size(); i++) {
Bitmap bitmap= BitmapFactory.decodeFile("/mnt/sdcard/resources/images/spelling/"+charList.get(i)+".png");
curentObjects=new SpellObjects(alphabetPoints,bitmap, i,charList.get(i), null, placeHolderBitmap.getHeight(), screenWidth/word.length(), true);
alphabetPoints.x=alphabetPoints.x+bitmap.getWidth();
spellObjectsList.add(curentObjects);
dragable_id_object_list.add(i, i);
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(imageBitmap, 0, 0, null);
for(SpellObjects currentObjects :spellObjectsList ){
if(currentObjects.getIsCorrectlyPlaced())
canvas.drawBitmap(currentObjects.getobjectBitmap(), currentObjects.getCurrentPoint().x,currentObjects.getCurrentPoint().y, null);
}
}
}
I want to render like cake,duck and good in other three quadrants.Actually this is spelling app for kids where they can drag and drop alphabets to respective positions.Please help me out I am stuck.
To set different images for your custom
Viewyou’ll need to add a custom attribute for your custom layout to signal the desired image(or an id, or something to uniquely identify the view). Something like this(inattr.xml):This attribute will be used in the layout like this:
Don’t forget to set the
newtagnamespace in the root of your xml layout:Then you’ll use that attribute to make the
SpellViewFirstuse the targeted image:Then you could use the
wordvariable to get the desiredBitmapinassignCoordinatesToImages(). Don’t forget to testwordfornull(meaning a picture name wasn’t set in the layout) and show a default image instead.